Skip to content

Commit

Permalink
refactor(ext/node): use concrete error types (denoland#26419)
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats authored Oct 24, 2024
1 parent b063cfe commit c71e020
Show file tree
Hide file tree
Showing 18 changed files with 674 additions and 305 deletions.
30 changes: 16 additions & 14 deletions ext/io/fs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use std::borrow::Cow;
use std::fmt::Formatter;
use std::io;
use std::rc::Rc;
use std::time::SystemTime;
Expand All @@ -21,6 +22,21 @@ pub enum FsError {
NotCapable(&'static str),
}

impl std::fmt::Display for FsError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
FsError::Io(err) => std::fmt::Display::fmt(err, f),
FsError::FileBusy => f.write_str("file busy"),
FsError::NotSupported => f.write_str("not supported"),
FsError::NotCapable(err) => {
f.write_str(&format!("requires {err} access"))
}
}
}
}

impl std::error::Error for FsError {}

impl FsError {
pub fn kind(&self) -> io::ErrorKind {
match self {
Expand Down Expand Up @@ -55,20 +71,6 @@ impl From<io::ErrorKind> for FsError {
}
}

impl From<FsError> for deno_core::error::AnyError {
fn from(err: FsError) -> Self {
match err {
FsError::Io(err) => err.into(),
FsError::FileBusy => deno_core::error::resource_unavailable(),
FsError::NotSupported => deno_core::error::not_supported(),
FsError::NotCapable(err) => deno_core::error::custom_error(
"NotCapable",
format!("permission denied: {err}"),
),
}
}
}

impl From<JoinError> for FsError {
fn from(err: JoinError) -> Self {
if err.is_cancelled() {
Expand Down
2 changes: 1 addition & 1 deletion ext/node/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use once_cell::sync::Lazy;
extern crate libz_sys as zlib;

mod global;
mod ops;
pub mod ops;
mod polyfill;

pub use deno_package_json::PackageJson;
Expand Down
53 changes: 35 additions & 18 deletions ext/node/ops/blocklist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ use std::net::Ipv4Addr;
use std::net::Ipv6Addr;
use std::net::SocketAddr;

use deno_core::anyhow::anyhow;
use deno_core::anyhow::bail;
use deno_core::error::AnyError;
use deno_core::op2;
use deno_core::OpState;

Expand All @@ -27,13 +24,25 @@ impl deno_core::GarbageCollected for BlockListResource {}
#[derive(Serialize)]
struct SocketAddressSerialization(String, String);

#[derive(Debug, thiserror::Error)]
pub enum BlocklistError {
#[error("{0}")]
AddrParse(#[from] std::net::AddrParseError),
#[error("{0}")]
IpNetwork(#[from] ipnetwork::IpNetworkError),
#[error("Invalid address")]
InvalidAddress,
#[error("IP version mismatch between start and end addresses")]
IpVersionMismatch,
}

#[op2(fast)]
pub fn op_socket_address_parse(
state: &mut OpState,
#[string] addr: &str,
#[smi] port: u16,
#[string] family: &str,
) -> Result<bool, AnyError> {
) -> Result<bool, BlocklistError> {
let ip = addr.parse::<IpAddr>()?;
let parsed: SocketAddr = SocketAddr::new(ip, port);
let parsed_ip_str = parsed.ip().to_string();
Expand All @@ -52,16 +61,16 @@ pub fn op_socket_address_parse(
Ok(false)
}
} else {
Err(anyhow!("Invalid address"))
Err(BlocklistError::InvalidAddress)
}
}

#[op2]
#[serde]
pub fn op_socket_address_get_serialization(
state: &mut OpState,
) -> Result<SocketAddressSerialization, AnyError> {
Ok(state.take::<SocketAddressSerialization>())
) -> SocketAddressSerialization {
state.take::<SocketAddressSerialization>()
}

#[op2]
Expand All @@ -77,7 +86,7 @@ pub fn op_blocklist_new() -> BlockListResource {
pub fn op_blocklist_add_address(
#[cppgc] wrap: &BlockListResource,
#[string] addr: &str,
) -> Result<(), AnyError> {
) -> Result<(), BlocklistError> {
wrap.blocklist.borrow_mut().add_address(addr)
}

Expand All @@ -86,7 +95,7 @@ pub fn op_blocklist_add_range(
#[cppgc] wrap: &BlockListResource,
#[string] start: &str,
#[string] end: &str,
) -> Result<bool, AnyError> {
) -> Result<bool, BlocklistError> {
wrap.blocklist.borrow_mut().add_range(start, end)
}

Expand All @@ -95,7 +104,7 @@ pub fn op_blocklist_add_subnet(
#[cppgc] wrap: &BlockListResource,
#[string] addr: &str,
#[smi] prefix: u8,
) -> Result<(), AnyError> {
) -> Result<(), BlocklistError> {
wrap.blocklist.borrow_mut().add_subnet(addr, prefix)
}

Expand All @@ -104,7 +113,7 @@ pub fn op_blocklist_check(
#[cppgc] wrap: &BlockListResource,
#[string] addr: &str,
#[string] r#type: &str,
) -> Result<bool, AnyError> {
) -> Result<bool, BlocklistError> {
wrap.blocklist.borrow().check(addr, r#type)
}

Expand All @@ -123,7 +132,7 @@ impl BlockList {
&mut self,
addr: IpAddr,
prefix: Option<u8>,
) -> Result<(), AnyError> {
) -> Result<(), BlocklistError> {
match addr {
IpAddr::V4(addr) => {
let ipv4_prefix = prefix.unwrap_or(32);
Expand Down Expand Up @@ -154,7 +163,7 @@ impl BlockList {
Ok(())
}

pub fn add_address(&mut self, address: &str) -> Result<(), AnyError> {
pub fn add_address(&mut self, address: &str) -> Result<(), BlocklistError> {
let ip: IpAddr = address.parse()?;
self.map_addr_add_network(ip, None)?;
Ok(())
Expand All @@ -164,7 +173,7 @@ impl BlockList {
&mut self,
start: &str,
end: &str,
) -> Result<bool, AnyError> {
) -> Result<bool, BlocklistError> {
let start_ip: IpAddr = start.parse()?;
let end_ip: IpAddr = end.parse()?;

Expand Down Expand Up @@ -193,25 +202,33 @@ impl BlockList {
self.map_addr_add_network(IpAddr::V6(addr), None)?;
}
}
_ => bail!("IP version mismatch between start and end addresses"),
_ => return Err(BlocklistError::IpVersionMismatch),
}
Ok(true)
}

pub fn add_subnet(&mut self, addr: &str, prefix: u8) -> Result<(), AnyError> {
pub fn add_subnet(
&mut self,
addr: &str,
prefix: u8,
) -> Result<(), BlocklistError> {
let ip: IpAddr = addr.parse()?;
self.map_addr_add_network(ip, Some(prefix))?;
Ok(())
}

pub fn check(&self, addr: &str, r#type: &str) -> Result<bool, AnyError> {
pub fn check(
&self,
addr: &str,
r#type: &str,
) -> Result<bool, BlocklistError> {
let addr: IpAddr = addr.parse()?;
let family = r#type.to_lowercase();
if family == "ipv4" && addr.is_ipv4() || family == "ipv6" && addr.is_ipv6()
{
Ok(self.rules.iter().any(|net| net.contains(addr)))
} else {
Err(anyhow!("Invalid address"))
Err(BlocklistError::InvalidAddress)
}
}
}
Expand Down
Loading

0 comments on commit c71e020

Please sign in to comment.