Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/node/internal/internal_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1049,14 +1049,20 @@ export class ERR_EINVAL extends NodeSyscallError {

export class ERR_EEXIST extends NodeSyscallError {
path?: string;
constructor(options: { syscall: string; path?: string }) {
const message = options.path
? `file already exists, ${options.syscall} '${options.path}'`
: 'file already exists';
dest?: string;
constructor(options: { syscall: string; path?: string; dest?: string }) {
let message = 'file already exists';
if (options.path !== undefined) {
message += `, ${options.syscall} '${options.path}'`;
if (options.dest !== undefined) message += ` -> '${options.dest}'`;
}
super('EEXIST', message, options.syscall);
if (options.path !== undefined) {
this.path = options.path;
}
if (options.dest !== undefined) {
this.dest = options.dest;
}
this.errno = -17; // EEXIST
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/node/internal/internal_fs_callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ export function copyFile(
if (mode & COPYFILE_EXCL && fssync.existsSync(dest)) {
throw new ERR_EEXIST({
syscall: 'copyFile',
path: normalizedDest.pathname,
path: normalizedSrc.pathname,
dest: normalizedDest.pathname,
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/node/internal/internal_fs_sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ export function copyFileSync(
if (mode & COPYFILE_EXCL && existsSync(dest)) {
throw new ERR_EEXIST({
syscall: 'copyFile',
path: normalizePath(dest).pathname,
path: normalizePath(src).pathname,
dest: normalizePath(dest).pathname,
});
}
cffs.renameOrCopy(normalizePath(src), normalizePath(dest), { copy: true });
Expand Down
25 changes: 25 additions & 0 deletions src/rust/node-exceptions/lib-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ fn uv_exception_formats_default_message_with_path() {
Some("no such file or directory, open '/tmp/missing'")
);
assert_eq!(get_string(lock, err, "code").as_deref(), Some("ENOENT"));
let errno = err.get(lock, "errno")?;
assert!(errno.is_int32());
assert_eq!(errno.int32_value(lock)?, -libc::ENOENT);
assert_eq!(get_string(lock, err, "syscall").as_deref(), Some("open"));
assert_eq!(
get_string(lock, err, "path").as_deref(),
Expand Down Expand Up @@ -115,6 +118,28 @@ fn uv_exception_uses_explicit_message_and_dest() {
});
}

#[test]
fn uv_exception_formats_default_message_with_dest() {
let harness = Harness::new();
harness.run_in_context(|lock, _ctx| {
let err = create_uv_exception_impl(
lock,
-libc::ENOENT,
"rename",
None,
Some(b"/a".as_slice()),
Some(b"/b".as_slice()),
)?;
assert_eq!(
get_string(lock, err, "message").as_deref(),
Some("no such file or directory, rename '/a' -> '/b'")
);
assert_eq!(get_string(lock, err, "path").as_deref(), Some("/a"));
assert_eq!(get_string(lock, err, "dest").as_deref(), Some("/b"));
Ok(())
});
}

#[test]
fn uv_exception_unknown_errno() {
let harness = Harness::new();
Expand Down
12 changes: 10 additions & 2 deletions src/rust/node-exceptions/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,9 @@ fn create_uv_exception_impl<'s>(
) -> jsg::Result<v8::Local<'s, v8::Object>> {
debug_assert!(!syscall.is_empty(), "syscall must not be null");

// Format the message to match Node.js format:
// "ENOENT: no such file or directory, open 'path'"
// Format the message like Node.js, minus its leading "ENOENT: " code prefix:
// "no such file or directory, open 'path'"
// with " -> 'dest'" appended when both path and dest are present.
//
// `path` may be arbitrary bytes, so the message is assembled as bytes and
// handed to V8 (which renders invalid UTF-8 lossily) rather than forced
Expand All @@ -265,12 +266,19 @@ fn create_uv_exception_impl<'s>(
msg.extend_from_slice(b" '");
msg.extend_from_slice(path);
msg.push(b'\'');
if let Some(dest) = dest {
msg.extend_from_slice(b" -> '");
msg.extend_from_slice(dest);
msg.push(b'\'');
}
}
msg
};

let obj = lock.error_from_bytes(&formatted_message);

let errno_value = v8::Number::new(lock, f64::from(errorno)).into();
Comment thread
guybedford marked this conversation as resolved.
Resolved
obj.set(lock, "errno", errno_value)?;
let syscall_value = bytes_to_value(lock, syscall.as_bytes());
obj.set(lock, "syscall", syscall_value)?;
let code_value = bytes_to_value(lock, uv_err_name(errorno).as_bytes());
Expand Down
Loading
Loading