Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node-compa: better error message #25921

Closed
familyboat opened this issue Sep 28, 2024 · 4 comments · Fixed by #26037
Closed

node-compa: better error message #25921

familyboat opened this issue Sep 28, 2024 · 4 comments · Fixed by #26037
Labels
bug Something isn't working correctly good first issue Good for newcomers node compat

Comments

@familyboat
Copy link
Contributor

familyboat commented Sep 28, 2024

Version: Deno 1.46.1

// main.ts
import fs from "node:fs";

/**
 * read file with node compa
 */
function readFileWithNode(file: string) {
  try {
    fs.statSync(file);
  } catch (error: unknown) {
    // No path information in error
    console.log(`Error occurred while reading file with node: ${error}`);
  }
}

/**
 * read file with deno
 */
function readFileWithDeno(file: string) {
  try {
    Deno.statSync(file);
  } catch (error: unknown) {
    // Has path information in error
    console.log(`Error occurred while reading file with deno: ${error}`);
  }
}

readFileWithNode("non-exist-file");
readFileWithDeno("non-exist-file");

Run below command:

deno run -A main.ts

The output is:

Error occurred while reading file with node: Error: ENOENT: no such file or directory, stat
Error occurred while reading file with deno: NotFound: 系统找不到指定的文件。 (os error 2): stat 'non-exist-file'

In this case, better error message(has path information) is useful for debug.

@lucacasonato lucacasonato added bug Something isn't working correctly good first issue Good for newcomers node compat labels Oct 1, 2024
@lucacasonato
Copy link
Member

I think there is some error wrapping going on here that needs to just be removed from the node:fs code path.

@familyboat
Copy link
Contributor Author

Do you think adding the missing file path information is sufficient for this case. If so, i can make a PR. @lucacasonato

@familyboat
Copy link
Contributor Author

It is my first time to contribute to deno. So i follow the instruction in here. After everything set up, run cargo build -vv. It exited in the fetching rust_v8 precompile stage(by the way, i am in China). So i can not make a PR.
If i am right, just addding path parameter in this line:

throw denoErrorToNodeError(err, { syscall: "stat" });
can fix this issue.

@familyboat
Copy link
Contributor Author

familyboat commented Oct 3, 2024

By using local version of rusty_v8 precompile, deno was built successfully.
I added the path parameter in here:

throw denoErrorToNodeError(err, { syscall: "stat" });

Change it to:

throw denoErrorToNodeError(err, { syscall: "stat", path: path.toString() });

Below is the test code:

import fs from "node:fs";

function readFileWithNode(file: string | URL) {
  const type = typeof file === "string" ? "string" : "url";

  try {
    fs.statSync(file);
  } catch (error: unknown) {
    console.log(
      `Error occurred while reading ${type} file with node: ${error}`,
    );
  }
}

function readFileWithDeno(file: string | URL) {
  const type = typeof file === "string" ? "string" : "url";

  try {
    Deno.statSync(file);
  } catch (error: unknown) {
    console.log(
      `Error occurred while reading ${type} file with deno: ${error}`,
    );
  }
}

const file = "non-exist-file";
const fileUrl = new URL(file, import.meta.url);
readFileWithNode(file);
readFileWithDeno(file);
readFileWithNode(fileUrl);
readFileWithDeno(fileUrl);

Output is:

Error occurred while reading string file with node: Error: ENOENT: no such file or directory, stat 'non-exist-file'
Error occurred while reading string file with deno: NotFound: No such file or directory (os error 2): stat 'non-exist-file'
Error occurred while reading url file with node: Error: ENOENT: no such file or directory, stat 'file:///root/test/node-file-error-message/non-exist-file'
Error occurred while reading url file with deno: NotFound: No such file or directory (os error 2): stat '/root/test/node-file-error-message/non-exist-file'

Do you think it is ok?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly good first issue Good for newcomers node compat
Projects
None yet
2 participants