diff options
author | Luca Casonato <hello@lcas.dev> | 2024-09-10 20:12:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-10 11:12:24 -0700 |
commit | 7bfcb4dd10d31f5f9566c90a28449c0951f3a48e (patch) | |
tree | fca0dec6e98118418f1712c6e8451a04c7e89988 /ext | |
parent | be5419d479fcae16c8a07dec00ce11b532b7996a (diff) |
feat(cli): use NotCapable error for permission errors (#25431)
Closes #7394
---------
Co-authored-by: snek <snek@deno.com>
Diffstat (limited to 'ext')
-rw-r--r-- | ext/fs/lib.rs | 6 | ||||
-rw-r--r-- | ext/fs/ops.rs | 4 | ||||
-rw-r--r-- | ext/io/fs.rs | 10 | ||||
-rw-r--r-- | ext/node/polyfills/_process/process.ts | 4 | ||||
-rw-r--r-- | ext/node/polyfills/internal_binding/pipe_wrap.ts | 22 | ||||
-rw-r--r-- | ext/node/polyfills/internal_binding/tcp_wrap.ts | 10 | ||||
-rw-r--r-- | ext/node/polyfills/internal_binding/udp_wrap.ts | 10 |
7 files changed, 20 insertions, 46 deletions
diff --git a/ext/fs/lib.rs b/ext/fs/lib.rs index 161eaa367..f4815fd29 100644 --- a/ext/fs/lib.rs +++ b/ext/fs/lib.rs @@ -91,7 +91,7 @@ impl FsPermissions for deno_permissions::PermissionsContainer { if resolved { self .check_special_file(path, api_name) - .map_err(FsError::PermissionDenied)?; + .map_err(FsError::NotCapable)?; return Ok(Cow::Borrowed(path)); } @@ -99,11 +99,11 @@ impl FsPermissions for deno_permissions::PermissionsContainer { let read = read || !write; if read { FsPermissions::check_read(self, path, api_name) - .map_err(|_| FsError::PermissionDenied("read"))?; + .map_err(|_| FsError::NotCapable("read"))?; } if write { FsPermissions::check_write(self, path, api_name) - .map_err(|_| FsError::PermissionDenied("write"))?; + .map_err(|_| FsError::NotCapable("write"))?; } Ok(Cow::Borrowed(path)) } diff --git a/ext/fs/ops.rs b/ext/fs/ops.rs index dc279b60d..8af2f0045 100644 --- a/ext/fs/ops.rs +++ b/ext/fs/ops.rs @@ -60,7 +60,7 @@ fn map_permission_error( path: &Path, ) -> AnyError { match error { - FsError::PermissionDenied(err) => { + FsError::NotCapable(err) => { let path = format!("{path:?}"); let (path, truncated) = if path.len() > 1024 { (&path[0..1024], "...(truncated)") @@ -74,7 +74,7 @@ fn map_permission_error( format!( "Requires {err} access to {path}{truncated}, run again with the --allow-{err} flag") }; - custom_error("PermissionDenied", msg) + custom_error("NotCapable", msg) } err => Err::<(), _>(err) .context_path(operation, path) diff --git a/ext/io/fs.rs b/ext/io/fs.rs index 88e4eee47..3798c1429 100644 --- a/ext/io/fs.rs +++ b/ext/io/fs.rs @@ -22,7 +22,7 @@ pub enum FsError { Io(io::Error), FileBusy, NotSupported, - PermissionDenied(&'static str), + NotCapable(&'static str), } impl FsError { @@ -31,7 +31,7 @@ impl FsError { Self::Io(err) => err.kind(), Self::FileBusy => io::ErrorKind::Other, Self::NotSupported => io::ErrorKind::Other, - Self::PermissionDenied(_) => io::ErrorKind::PermissionDenied, + Self::NotCapable(_) => io::ErrorKind::Other, } } @@ -40,7 +40,7 @@ impl FsError { FsError::Io(err) => err, FsError::FileBusy => io::Error::new(self.kind(), "file busy"), FsError::NotSupported => io::Error::new(self.kind(), "not supported"), - FsError::PermissionDenied(err) => { + FsError::NotCapable(err) => { io::Error::new(self.kind(), format!("requires {err} access")) } } @@ -65,8 +65,8 @@ impl From<FsError> for AnyError { FsError::Io(err) => AnyError::from(err), FsError::FileBusy => resource_unavailable(), FsError::NotSupported => not_supported(), - FsError::PermissionDenied(err) => { - custom_error("PermissionDenied", format!("permission denied: {err}")) + FsError::NotCapable(err) => { + custom_error("NotCapable", format!("permission denied: {err}")) } } } diff --git a/ext/node/polyfills/_process/process.ts b/ext/node/polyfills/_process/process.ts index e4b88a11a..6f69139c9 100644 --- a/ext/node/polyfills/_process/process.ts +++ b/ext/node/polyfills/_process/process.ts @@ -53,8 +53,8 @@ function denoEnvGet(name: string) { } catch (e) { if ( ObjectPrototypeIsPrototypeOf(TypeErrorPrototype, e) || - // TODO(iuioiua): Use `PermissionDeniedPrototype` when it's available - ObjectPrototypeIsPrototypeOf(Deno.errors.PermissionDenied.prototype, e) + // TODO(iuioiua): Use `NotCapablePrototype` when it's available + ObjectPrototypeIsPrototypeOf(Deno.errors.NotCapable.prototype, e) ) { return undefined; } diff --git a/ext/node/polyfills/internal_binding/pipe_wrap.ts b/ext/node/polyfills/internal_binding/pipe_wrap.ts index f5c3c5439..3e366b3c7 100644 --- a/ext/node/polyfills/internal_binding/pipe_wrap.ts +++ b/ext/node/polyfills/internal_binding/pipe_wrap.ts @@ -160,16 +160,8 @@ export class Pipe extends ConnectionWrap { } }, (e) => { - // TODO(cmorten): correct mapping of connection error to status code. - let code: number; - - if (e instanceof Deno.errors.NotFound) { - code = codeMap.get("ENOENT")!; - } else if (e instanceof Deno.errors.PermissionDenied) { - code = codeMap.get("EACCES")!; - } else { - code = codeMap.get("ECONNREFUSED")!; - } + const code = codeMap.get(e.code ?? "UNKNOWN") ?? + codeMap.get("UNKNOWN")!; try { this.afterConnect(req, code); @@ -207,16 +199,10 @@ export class Pipe extends ConnectionWrap { try { listener = Deno.listen(listenOptions); } catch (e) { - if (e instanceof Deno.errors.AddrInUse) { - return codeMap.get("EADDRINUSE")!; - } else if (e instanceof Deno.errors.AddrNotAvailable) { - return codeMap.get("EADDRNOTAVAIL")!; - } else if (e instanceof Deno.errors.PermissionDenied) { + if (e instanceof Deno.errors.NotCapable) { throw e; } - - // TODO(cmorten): map errors to appropriate error codes. - return codeMap.get("UNKNOWN")!; + return codeMap.get(e.code ?? "UNKNOWN") ?? codeMap.get("UNKNOWN")!; } const address = listener.addr as Deno.UnixAddr; diff --git a/ext/node/polyfills/internal_binding/tcp_wrap.ts b/ext/node/polyfills/internal_binding/tcp_wrap.ts index 4b57a7e1e..973a1d1c0 100644 --- a/ext/node/polyfills/internal_binding/tcp_wrap.ts +++ b/ext/node/polyfills/internal_binding/tcp_wrap.ts @@ -212,16 +212,10 @@ export class TCP extends ConnectionWrap { try { listener = Deno.listen(listenOptions); } catch (e) { - if (e instanceof Deno.errors.AddrInUse) { - return codeMap.get("EADDRINUSE")!; - } else if (e instanceof Deno.errors.AddrNotAvailable) { - return codeMap.get("EADDRNOTAVAIL")!; - } else if (e instanceof Deno.errors.PermissionDenied) { + if (e instanceof Deno.errors.NotCapable) { throw e; } - - // TODO(cmorten): map errors to appropriate error codes. - return codeMap.get("UNKNOWN")!; + return codeMap.get(e.code ?? "UNKNOWN") ?? codeMap.get("UNKNOWN")!; } const address = listener.addr as Deno.NetAddr; diff --git a/ext/node/polyfills/internal_binding/udp_wrap.ts b/ext/node/polyfills/internal_binding/udp_wrap.ts index 7cbd6cabe..db6961ddb 100644 --- a/ext/node/polyfills/internal_binding/udp_wrap.ts +++ b/ext/node/polyfills/internal_binding/udp_wrap.ts @@ -337,16 +337,10 @@ export class UDP extends HandleWrap { try { listener = DenoListenDatagram(listenOptions); } catch (e) { - if (e instanceof Deno.errors.AddrInUse) { - return codeMap.get("EADDRINUSE")!; - } else if (e instanceof Deno.errors.AddrNotAvailable) { - return codeMap.get("EADDRNOTAVAIL")!; - } else if (e instanceof Deno.errors.PermissionDenied) { + if (e instanceof Deno.errors.NotCapable) { throw e; } - - // TODO(cmorten): map errors to appropriate error codes. - return codeMap.get("UNKNOWN")!; + return codeMap.get(e.code ?? "UNKNOWN") ?? codeMap.get("UNKNOWN")!; } const address = listener.addr as Deno.NetAddr; |