deno/cli/lib/util/result.rs
Leo Kettmeir 59f77c909e
Some checks are pending
ci / publish canary (push) Blocked by required conditions
ci / pre-build (push) Waiting to run
ci / test debug linux-aarch64 (push) Blocked by required conditions
ci / test release linux-aarch64 (push) Blocked by required conditions
ci / test debug macos-aarch64 (push) Blocked by required conditions
ci / test release macos-aarch64 (push) Blocked by required conditions
ci / bench release linux-x86_64 (push) Blocked by required conditions
ci / lint debug linux-x86_64 (push) Blocked by required conditions
ci / lint debug macos-x86_64 (push) Blocked by required conditions
ci / lint debug windows-x86_64 (push) Blocked by required conditions
ci / test debug linux-x86_64 (push) Blocked by required conditions
ci / test release linux-x86_64 (push) Blocked by required conditions
ci / test debug macos-x86_64 (push) Blocked by required conditions
ci / test release macos-x86_64 (push) Blocked by required conditions
ci / test debug windows-x86_64 (push) Blocked by required conditions
ci / test release windows-x86_64 (push) Blocked by required conditions
ci / build libs (push) Blocked by required conditions
refactor(webgpu): make cppgc arguments safer (#30563)
Also updates `deno_core`.

---------

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
2025-09-03 18:01:58 +02:00

71 lines
1.7 KiB
Rust

// Copyright 2018-2025 the Deno authors. MIT license.
use std::convert::Infallible;
use deno_error::JsErrorBox;
use deno_error::JsErrorClass;
use deno_resolver::DenoResolveError;
use deno_resolver::DenoResolveErrorKind;
use deno_runtime::deno_core::error::AnyError;
use deno_runtime::deno_core::error::CoreError;
use deno_runtime::deno_core::error::CoreErrorKind;
pub trait InfallibleResultExt<T> {
fn unwrap_infallible(self) -> T;
}
impl<T> InfallibleResultExt<T> for Result<T, Infallible> {
fn unwrap_infallible(self) -> T {
match self {
Ok(value) => value,
Err(never) => match never {},
}
}
}
pub fn js_error_downcast_ref(
err: &AnyError,
) -> Option<&deno_runtime::deno_core::error::JsError> {
any_and_jserrorbox_downcast_ref(err)
.or_else(|| {
err
.downcast_ref::<CoreError>()
.and_then(|e| match e.as_kind() {
CoreErrorKind::Js(e) => Some(e),
_ => None,
})
})
.map(|v| &**v)
}
pub fn any_and_jserrorbox_downcast_ref<
E: std::error::Error + Send + Sync + 'static,
>(
err: &AnyError,
) -> Option<&E> {
err
.downcast_ref::<E>()
.or_else(|| {
err
.downcast_ref::<JsErrorBox>()
.and_then(|e| e.get_ref().downcast_ref::<E>())
})
.or_else(|| {
err
.downcast_ref::<CoreError>()
.and_then(|e| match e.as_kind() {
CoreErrorKind::JsBox(e) => e.get_ref().downcast_ref::<E>(),
_ => None,
})
})
}
pub fn downcast_ref_deno_resolve_error(
err: &JsErrorBox,
) -> Option<&DenoResolveErrorKind> {
err
.get_ref()
.downcast_ref::<DenoResolveError>()
.map(|e| e.as_kind())
.or_else(|| err.get_ref().downcast_ref::<DenoResolveErrorKind>())
}