koii-network / ezsandbox

The best place to get started with all things decentralized
49 stars 24 forks source link

Create the directory before creating the file #39

Closed marsrobertson closed 2 months ago

marsrobertson commented 3 months ago

Error I was getting:

Log file not found, creating /Users/m/Library/Application Support/KOII-Desktop-Node/namespace/AK2P1L8NWGwWarbHeM7tX2mr4hJA7ZVXGSSSz5PWHBHv/task.log
node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[Error: ENOENT: no such file or directory, open '/Users/m/Library/Application Support/KOII-Desktop-Node/namespace/AK2P1L8NWGwWarbHeM7tX2mr4hJA7ZVXGSSSz5PWHBHv/task.log'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/Users/m/Library/Application Support/KOII-Desktop-Node/namespace/AK2P1L8NWGwWarbHeM7tX2mr4hJA7ZVXGSSSz5PWHBHv/task.log'
}

Node.js v18.20.3
[nodemon] app crashed - waiting for file changes before starting..

Here: https://github.com/koii-network/ezsandbox/blob/53ca97a2e04f2667229fb08e1bd227bd4ef4fc33/Lesson%201/EZ-testing-task/prod-debug.js#L63-L69

Need to add:

    // Extract the directory path from the full log file path
    const dirPath = path.dirname(desktopNodeLogPath);

    // Check if the directory exists, create it if it doesn't
    try {
      await fs.promises.access(dirPath, fs.constants.F_OK);
    } catch (dirErr) {
      console.log(`Directory not found, creating ${dirPath}`);
      await fs.promises.mkdir(dirPath, { recursive: true });
    }

And on top of the file: const path = require('path');

marsrobertson commented 3 months ago

Same issue persists in other prod-debug.js files:

https://github.com/koii-network/ezsandbox/blob/efafc2dc63fbd03e8b73e9a882315ea1464be396/Lesson%202/upnp-basics/after/prod-debug.js#L63-L69

Presumably all of them needs updating.

marsrobertson commented 3 months ago

Some of it has been fixed here: https://github.com/labrocadabro/ezsandbox/pull/1/files

labrocadabro commented 2 months ago

I've made a merge request to the task template (which is where this needs to be changed). But instead of creating the directory, prod-debug will provide a more informative message and exit.

This is because there are two reasons this error usually occurs - one is running prod-debug before adding the task to the desktop node. This fix sort of deals with that but it's wonky - when I tested this and added the task after running prod-debug, I couldn't see the task until I restarted the node.

But the second reason it occurs is that the developer is using the wrong task ID. Creating a directory in that situation wouldn't help them diagnose the problem.

marsrobertson commented 2 months ago

This is because there are two reasons this error usually occurs - one is running prod-debug before adding the task to the desktop node.

I thought that's the default way... Run prod-debug first to build / compile in order obtain the task ID.

Bottom line: that should be smooth, simple, intuitive, just works :)