1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

refactor(ext/url): use concrete error types (#26172)

This commit is contained in:
Leo Kettmeir 2024-10-14 14:15:31 -07:00 committed by GitHub
parent cb385d9e4a
commit 48cbf85add
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 22 additions and 14 deletions

1
Cargo.lock generated
View file

@ -2163,6 +2163,7 @@ dependencies = [
"deno_console",
"deno_core",
"deno_webidl",
"thiserror",
"urlpattern",
]

View file

@ -15,6 +15,7 @@ path = "lib.rs"
[dependencies]
deno_core.workspace = true
thiserror.workspace = true
urlpattern = "0.3.0"
[dev-dependencies]

View file

@ -15,6 +15,8 @@ use std::path::PathBuf;
use crate::urlpattern::op_urlpattern_parse;
use crate::urlpattern::op_urlpattern_process_match_input;
pub use urlpattern::UrlPatternError;
deno_core::extension!(
deno_url,
deps = [deno_webidl],

View file

@ -1,7 +1,5 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::op2;
use urlpattern::quirks;
@ -9,21 +7,23 @@ use urlpattern::quirks::MatchInput;
use urlpattern::quirks::StringOrInit;
use urlpattern::quirks::UrlPattern;
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct UrlPatternError(urlpattern::Error);
#[op2]
#[serde]
pub fn op_urlpattern_parse(
#[serde] input: StringOrInit,
#[string] base_url: Option<String>,
#[serde] options: urlpattern::UrlPatternOptions,
) -> Result<UrlPattern, AnyError> {
let init = urlpattern::quirks::process_construct_pattern_input(
input,
base_url.as_deref(),
)
.map_err(|e| type_error(e.to_string()))?;
) -> Result<UrlPattern, UrlPatternError> {
let init =
quirks::process_construct_pattern_input(input, base_url.as_deref())
.map_err(UrlPatternError)?;
let pattern = urlpattern::quirks::parse_pattern(init, options)
.map_err(|e| type_error(e.to_string()))?;
let pattern =
quirks::parse_pattern(init, options).map_err(UrlPatternError)?;
Ok(pattern)
}
@ -33,14 +33,14 @@ pub fn op_urlpattern_parse(
pub fn op_urlpattern_process_match_input(
#[serde] input: StringOrInit,
#[string] base_url: Option<String>,
) -> Result<Option<(MatchInput, quirks::Inputs)>, AnyError> {
let res = urlpattern::quirks::process_match_input(input, base_url.as_deref())
.map_err(|e| type_error(e.to_string()))?;
) -> Result<Option<(MatchInput, quirks::Inputs)>, UrlPatternError> {
let res = quirks::process_match_input(input, base_url.as_deref())
.map_err(UrlPatternError)?;
let (input, inputs) = match res {
Some((input, inputs)) => (input, inputs),
None => return Ok(None),
};
Ok(urlpattern::quirks::parse_match_input(input).map(|input| (input, inputs)))
Ok(quirks::parse_match_input(input).map(|input| (input, inputs)))
}

View file

@ -244,6 +244,10 @@ pub fn get_error_class_name(e: &AnyError) -> Option<&'static str> {
e.downcast_ref::<WebStorageError>()
.map(get_webstorage_class_name)
})
.or_else(|| {
e.downcast_ref::<deno_url::UrlPatternError>()
.map(|_| "TypeError")
})
.or_else(|| {
e.downcast_ref::<dlopen2::Error>()
.map(get_dlopen_error_class)