refactor(ext/web): use concrete error types (#26185)

This commit is contained in:
Leo Kettmeir 2024-10-17 12:05:38 -07:00 committed by GitHub
parent 40b4b9aaa3
commit eca83fc9b4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 257 additions and 184 deletions

View file

@ -28,6 +28,11 @@ use deno_kv::KvError;
use deno_kv::KvMutationError;
use deno_net::ops::NetError;
use deno_tls::TlsError;
use deno_web::BlobError;
use deno_web::CompressionError;
use deno_web::MessagePortError;
use deno_web::StreamResourceError;
use deno_web::WebError;
use deno_webstorage::WebStorageError;
use std::env;
use std::error::Error;
@ -169,6 +174,61 @@ pub fn get_nix_error_class(error: &nix::Error) -> &'static str {
}
}
fn get_web_error_class(e: &WebError) -> &'static str {
match e {
WebError::Base64Decode => "DOMExceptionInvalidCharacterError",
WebError::InvalidEncodingLabel(_) => "RangeError",
WebError::BufferTooLong => "TypeError",
WebError::ValueTooLarge => "RangeError",
WebError::BufferTooSmall => "RangeError",
WebError::DataInvalid => "TypeError",
WebError::DataError(_) => "Error",
}
}
fn get_web_compression_error_class(e: &CompressionError) -> &'static str {
match e {
CompressionError::UnsupportedFormat => "TypeError",
CompressionError::ResourceClosed => "TypeError",
CompressionError::IoTypeError(_) => "TypeError",
CompressionError::Io(e) => get_io_error_class(e),
}
}
fn get_web_message_port_error_class(e: &MessagePortError) -> &'static str {
match e {
MessagePortError::InvalidTransfer => "TypeError",
MessagePortError::NotReady => "TypeError",
MessagePortError::TransferSelf => "TypeError",
MessagePortError::Canceled(e) => {
let io_err: io::Error = e.to_owned().into();
get_io_error_class(&io_err)
}
MessagePortError::Resource(e) => get_error_class_name(e).unwrap_or("Error"),
}
}
fn get_web_stream_resource_error_class(
e: &StreamResourceError,
) -> &'static str {
match e {
StreamResourceError::Canceled(e) => {
let io_err: io::Error = e.to_owned().into();
get_io_error_class(&io_err)
}
StreamResourceError::Js(_) => "TypeError",
}
}
fn get_web_blob_error_class(e: &BlobError) -> &'static str {
match e {
BlobError::BlobPartNotFound => "TypeError",
BlobError::SizeLargerThanBlobPart => "TypeError",
BlobError::BlobURLsNotSupported => "TypeError",
BlobError::Url(_) => "Error",
}
}
fn get_ffi_repr_error_class(e: &ReprError) -> &'static str {
match e {
ReprError::InvalidOffset => "TypeError",
@ -382,8 +442,21 @@ fn get_net_map_error(error: &deno_net::io::MapError) -> &'static str {
pub fn get_error_class_name(e: &AnyError) -> Option<&'static str> {
deno_core::error::get_custom_error_class(e)
.or_else(|| deno_webgpu::error::get_error_class_name(e))
.or_else(|| deno_web::get_error_class_name(e))
.or_else(|| deno_websocket::get_network_error_class_name(e))
.or_else(|| e.downcast_ref::<WebError>().map(get_web_error_class))
.or_else(|| {
e.downcast_ref::<CompressionError>()
.map(get_web_compression_error_class)
})
.or_else(|| {
e.downcast_ref::<MessagePortError>()
.map(get_web_message_port_error_class)
})
.or_else(|| {
e.downcast_ref::<StreamResourceError>()
.map(get_web_stream_resource_error_class)
})
.or_else(|| e.downcast_ref::<BlobError>().map(get_web_blob_error_class))
.or_else(|| e.downcast_ref::<IRError>().map(|_| "TypeError"))
.or_else(|| e.downcast_ref::<ReprError>().map(get_ffi_repr_error_class))
.or_else(|| {