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:
parent
cb385d9e4a
commit
48cbf85add
5 changed files with 22 additions and 14 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -2163,6 +2163,7 @@ dependencies = [
|
||||||
"deno_console",
|
"deno_console",
|
||||||
"deno_core",
|
"deno_core",
|
||||||
"deno_webidl",
|
"deno_webidl",
|
||||||
|
"thiserror",
|
||||||
"urlpattern",
|
"urlpattern",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ path = "lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
deno_core.workspace = true
|
deno_core.workspace = true
|
||||||
|
thiserror.workspace = true
|
||||||
urlpattern = "0.3.0"
|
urlpattern = "0.3.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
|
@ -15,6 +15,8 @@ use std::path::PathBuf;
|
||||||
use crate::urlpattern::op_urlpattern_parse;
|
use crate::urlpattern::op_urlpattern_parse;
|
||||||
use crate::urlpattern::op_urlpattern_process_match_input;
|
use crate::urlpattern::op_urlpattern_process_match_input;
|
||||||
|
|
||||||
|
pub use urlpattern::UrlPatternError;
|
||||||
|
|
||||||
deno_core::extension!(
|
deno_core::extension!(
|
||||||
deno_url,
|
deno_url,
|
||||||
deps = [deno_webidl],
|
deps = [deno_webidl],
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
// 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 deno_core::op2;
|
||||||
|
|
||||||
use urlpattern::quirks;
|
use urlpattern::quirks;
|
||||||
|
@ -9,21 +7,23 @@ use urlpattern::quirks::MatchInput;
|
||||||
use urlpattern::quirks::StringOrInit;
|
use urlpattern::quirks::StringOrInit;
|
||||||
use urlpattern::quirks::UrlPattern;
|
use urlpattern::quirks::UrlPattern;
|
||||||
|
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
#[error(transparent)]
|
||||||
|
pub struct UrlPatternError(urlpattern::Error);
|
||||||
|
|
||||||
#[op2]
|
#[op2]
|
||||||
#[serde]
|
#[serde]
|
||||||
pub fn op_urlpattern_parse(
|
pub fn op_urlpattern_parse(
|
||||||
#[serde] input: StringOrInit,
|
#[serde] input: StringOrInit,
|
||||||
#[string] base_url: Option<String>,
|
#[string] base_url: Option<String>,
|
||||||
#[serde] options: urlpattern::UrlPatternOptions,
|
#[serde] options: urlpattern::UrlPatternOptions,
|
||||||
) -> Result<UrlPattern, AnyError> {
|
) -> Result<UrlPattern, UrlPatternError> {
|
||||||
let init = urlpattern::quirks::process_construct_pattern_input(
|
let init =
|
||||||
input,
|
quirks::process_construct_pattern_input(input, base_url.as_deref())
|
||||||
base_url.as_deref(),
|
.map_err(UrlPatternError)?;
|
||||||
)
|
|
||||||
.map_err(|e| type_error(e.to_string()))?;
|
|
||||||
|
|
||||||
let pattern = urlpattern::quirks::parse_pattern(init, options)
|
let pattern =
|
||||||
.map_err(|e| type_error(e.to_string()))?;
|
quirks::parse_pattern(init, options).map_err(UrlPatternError)?;
|
||||||
|
|
||||||
Ok(pattern)
|
Ok(pattern)
|
||||||
}
|
}
|
||||||
|
@ -33,14 +33,14 @@ pub fn op_urlpattern_parse(
|
||||||
pub fn op_urlpattern_process_match_input(
|
pub fn op_urlpattern_process_match_input(
|
||||||
#[serde] input: StringOrInit,
|
#[serde] input: StringOrInit,
|
||||||
#[string] base_url: Option<String>,
|
#[string] base_url: Option<String>,
|
||||||
) -> Result<Option<(MatchInput, quirks::Inputs)>, AnyError> {
|
) -> Result<Option<(MatchInput, quirks::Inputs)>, UrlPatternError> {
|
||||||
let res = urlpattern::quirks::process_match_input(input, base_url.as_deref())
|
let res = quirks::process_match_input(input, base_url.as_deref())
|
||||||
.map_err(|e| type_error(e.to_string()))?;
|
.map_err(UrlPatternError)?;
|
||||||
|
|
||||||
let (input, inputs) = match res {
|
let (input, inputs) = match res {
|
||||||
Some((input, inputs)) => (input, inputs),
|
Some((input, inputs)) => (input, inputs),
|
||||||
None => return Ok(None),
|
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)))
|
||||||
}
|
}
|
||||||
|
|
|
@ -244,6 +244,10 @@ pub fn get_error_class_name(e: &AnyError) -> Option<&'static str> {
|
||||||
e.downcast_ref::<WebStorageError>()
|
e.downcast_ref::<WebStorageError>()
|
||||||
.map(get_webstorage_class_name)
|
.map(get_webstorage_class_name)
|
||||||
})
|
})
|
||||||
|
.or_else(|| {
|
||||||
|
e.downcast_ref::<deno_url::UrlPatternError>()
|
||||||
|
.map(|_| "TypeError")
|
||||||
|
})
|
||||||
.or_else(|| {
|
.or_else(|| {
|
||||||
e.downcast_ref::<dlopen2::Error>()
|
e.downcast_ref::<dlopen2::Error>()
|
||||||
.map(get_dlopen_error_class)
|
.map(get_dlopen_error_class)
|
||||||
|
|
Loading…
Reference in a new issue