diff options
author | Leo Kettmeir <crowlkats@toaxl.com> | 2024-10-24 10:45:17 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-24 10:45:17 -0700 |
commit | c71e020668b40666aecfdffb1dbf979abcb41958 (patch) | |
tree | 6b963905f50c17c21d9a89a5f5f7eee2fa83e808 /ext/node/ops/os/mod.rs | |
parent | b063cfecfe0479b1de14b8e9e06f1921ce830127 (diff) |
refactor(ext/node): use concrete error types (#26419)
Diffstat (limited to 'ext/node/ops/os/mod.rs')
-rw-r--r-- | ext/node/ops/os/mod.rs | 54 |
1 files changed, 38 insertions, 16 deletions
diff --git a/ext/node/ops/os/mod.rs b/ext/node/ops/os/mod.rs index ca91895f2..b4c9eaa8c 100644 --- a/ext/node/ops/os/mod.rs +++ b/ext/node/ops/os/mod.rs @@ -1,28 +1,38 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. use crate::NodePermissions; -use deno_core::error::type_error; -use deno_core::error::AnyError; use deno_core::op2; use deno_core::OpState; mod cpus; -mod priority; +pub mod priority; + +#[derive(Debug, thiserror::Error)] +pub enum OsError { + #[error(transparent)] + Priority(priority::PriorityError), + #[error(transparent)] + Permission(deno_core::error::AnyError), + #[error("Failed to get cpu info")] + FailedToGetCpuInfo, +} #[op2(fast)] pub fn op_node_os_get_priority<P>( state: &mut OpState, pid: u32, -) -> Result<i32, AnyError> +) -> Result<i32, OsError> where P: NodePermissions + 'static, { { let permissions = state.borrow_mut::<P>(); - permissions.check_sys("getPriority", "node:os.getPriority()")?; + permissions + .check_sys("getPriority", "node:os.getPriority()") + .map_err(OsError::Permission)?; } - priority::get_priority(pid) + priority::get_priority(pid).map_err(OsError::Priority) } #[op2(fast)] @@ -30,21 +40,25 @@ pub fn op_node_os_set_priority<P>( state: &mut OpState, pid: u32, priority: i32, -) -> Result<(), AnyError> +) -> Result<(), OsError> where P: NodePermissions + 'static, { { let permissions = state.borrow_mut::<P>(); - permissions.check_sys("setPriority", "node:os.setPriority()")?; + permissions + .check_sys("setPriority", "node:os.setPriority()") + .map_err(OsError::Permission)?; } - priority::set_priority(pid, priority) + priority::set_priority(pid, priority).map_err(OsError::Priority) } #[op2] #[string] -pub fn op_node_os_username<P>(state: &mut OpState) -> Result<String, AnyError> +pub fn op_node_os_username<P>( + state: &mut OpState, +) -> Result<String, deno_core::error::AnyError> where P: NodePermissions + 'static, { @@ -57,7 +71,9 @@ where } #[op2(fast)] -pub fn op_geteuid<P>(state: &mut OpState) -> Result<u32, AnyError> +pub fn op_geteuid<P>( + state: &mut OpState, +) -> Result<u32, deno_core::error::AnyError> where P: NodePermissions + 'static, { @@ -76,7 +92,9 @@ where } #[op2(fast)] -pub fn op_getegid<P>(state: &mut OpState) -> Result<u32, AnyError> +pub fn op_getegid<P>( + state: &mut OpState, +) -> Result<u32, deno_core::error::AnyError> where P: NodePermissions + 'static, { @@ -96,21 +114,25 @@ where #[op2] #[serde] -pub fn op_cpus<P>(state: &mut OpState) -> Result<Vec<cpus::CpuInfo>, AnyError> +pub fn op_cpus<P>(state: &mut OpState) -> Result<Vec<cpus::CpuInfo>, OsError> where P: NodePermissions + 'static, { { let permissions = state.borrow_mut::<P>(); - permissions.check_sys("cpus", "node:os.cpus()")?; + permissions + .check_sys("cpus", "node:os.cpus()") + .map_err(OsError::Permission)?; } - cpus::cpu_info().ok_or_else(|| type_error("Failed to get cpu info")) + cpus::cpu_info().ok_or(OsError::FailedToGetCpuInfo) } #[op2] #[string] -pub fn op_homedir<P>(state: &mut OpState) -> Result<Option<String>, AnyError> +pub fn op_homedir<P>( + state: &mut OpState, +) -> Result<Option<String>, deno_core::error::AnyError> where P: NodePermissions + 'static, { |