refactor: update deno_core for error refactor (#26867)

Closes #26171

---------

Co-authored-by: David Sherret <dsherret@gmail.com>
This commit is contained in:
Leo Kettmeir 2025-01-08 14:52:32 -08:00 committed by GitHub
parent 814da49dff
commit ea30e188a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
214 changed files with 3787 additions and 4210 deletions

View file

@ -46,6 +46,7 @@ use deno_core::OpState;
use deno_core::RcRef;
use deno_core::Resource;
use deno_core::ResourceId;
use deno_error::JsErrorBox;
use deno_path_util::url_from_file_path;
use deno_path_util::PathToUrlError;
use deno_permissions::PermissionCheckError;
@ -100,9 +101,8 @@ pub struct Options {
/// For more info on what can be configured, see [`hyper_util::client::legacy::Builder`].
pub client_builder_hook: Option<fn(HyperClientBuilder) -> HyperClientBuilder>,
#[allow(clippy::type_complexity)]
pub request_builder_hook: Option<
fn(&mut http::Request<ReqBody>) -> Result<(), deno_core::error::AnyError>,
>,
pub request_builder_hook:
Option<fn(&mut http::Request<ReqBody>) -> Result<(), JsErrorBox>>,
pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
pub client_cert_chain_and_key: TlsKeys,
pub file_fetch_handler: Rc<dyn FetchHandler>,
@ -110,9 +110,7 @@ pub struct Options {
}
impl Options {
pub fn root_cert_store(
&self,
) -> Result<Option<RootCertStore>, deno_core::error::AnyError> {
pub fn root_cert_store(&self) -> Result<Option<RootCertStore>, JsErrorBox> {
Ok(match &self.root_cert_store_provider {
Some(provider) => Some(provider.get_or_try_init()?.clone()),
None => None,
@ -164,48 +162,71 @@ deno_core::extension!(deno_fetch,
},
);
#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, deno_error::JsError)]
pub enum FetchError {
#[class(inherit)]
#[error(transparent)]
Resource(deno_core::error::AnyError),
Resource(#[from] deno_core::error::ResourceError),
#[class(inherit)]
#[error(transparent)]
Permission(#[from] PermissionCheckError),
#[class(type)]
#[error("NetworkError when attempting to fetch resource")]
NetworkError,
#[class(type)]
#[error("Fetching files only supports the GET method: received {0}")]
FsNotGet(Method),
#[class(inherit)]
#[error(transparent)]
PathToUrl(#[from] PathToUrlError),
#[class(type)]
#[error("Invalid URL {0}")]
InvalidUrl(Url),
#[class(type)]
#[error(transparent)]
InvalidHeaderName(#[from] http::header::InvalidHeaderName),
#[class(type)]
#[error(transparent)]
InvalidHeaderValue(#[from] http::header::InvalidHeaderValue),
#[class(type)]
#[error("{0:?}")]
DataUrl(data_url::DataUrlError),
#[class(type)]
#[error("{0:?}")]
Base64(data_url::forgiving_base64::InvalidBase64),
#[class(type)]
#[error("Blob for the given URL not found.")]
BlobNotFound,
#[class(type)]
#[error("Url scheme '{0}' not supported")]
SchemeNotSupported(String),
#[class(type)]
#[error("Request was cancelled")]
RequestCanceled,
#[class(generic)]
#[error(transparent)]
Http(#[from] http::Error),
#[class(inherit)]
#[error(transparent)]
ClientCreate(#[from] HttpClientCreateError),
#[class(inherit)]
#[error(transparent)]
Url(#[from] url::ParseError),
#[class(type)]
#[error(transparent)]
Method(#[from] http::method::InvalidMethod),
#[class(inherit)]
#[error(transparent)]
ClientSend(#[from] ClientSendError),
#[class(inherit)]
#[error(transparent)]
RequestBuilderHook(deno_core::error::AnyError),
RequestBuilderHook(JsErrorBox),
#[class(inherit)]
#[error(transparent)]
Io(#[from] std::io::Error),
#[class(generic)]
#[error(transparent)]
Dns(hickory_resolver::ResolveError),
}
pub type CancelableResponseFuture =
@ -294,9 +315,7 @@ pub fn create_client_from_options(
#[allow(clippy::type_complexity)]
pub struct ResourceToBodyAdapter(
Rc<dyn Resource>,
Option<
Pin<Box<dyn Future<Output = Result<BufView, deno_core::error::AnyError>>>>,
>,
Option<Pin<Box<dyn Future<Output = Result<BufView, JsErrorBox>>>>>,
);
impl ResourceToBodyAdapter {
@ -312,7 +331,7 @@ unsafe impl Send for ResourceToBodyAdapter {}
unsafe impl Sync for ResourceToBodyAdapter {}
impl Stream for ResourceToBodyAdapter {
type Item = Result<Bytes, deno_core::error::AnyError>;
type Item = Result<Bytes, JsErrorBox>;
fn poll_next(
self: Pin<&mut Self>,
@ -342,7 +361,7 @@ impl Stream for ResourceToBodyAdapter {
impl hyper::body::Body for ResourceToBodyAdapter {
type Data = Bytes;
type Error = deno_core::error::AnyError;
type Error = JsErrorBox;
fn poll_frame(
self: Pin<&mut Self>,
@ -417,10 +436,7 @@ where
FP: FetchPermissions + 'static,
{
let (client, allow_host) = if let Some(rid) = client_rid {
let r = state
.resource_table
.get::<HttpClientResource>(rid)
.map_err(FetchError::Resource)?;
let r = state.resource_table.get::<HttpClientResource>(rid)?;
(r.client.clone(), r.allow_host)
} else {
(get_or_create_client_from_state(state)?, false)
@ -479,10 +495,7 @@ where
ReqBody::full(data.to_vec().into())
}
(_, Some(resource)) => {
let resource = state
.resource_table
.take_any(resource)
.map_err(FetchError::Resource)?;
let resource = state.resource_table.take_any(resource)?;
match resource.size_hint() {
(body_size, Some(n)) if body_size == n && body_size > 0 => {
con_len = Some(body_size);
@ -624,8 +637,7 @@ pub async fn op_fetch_send(
let request = state
.borrow_mut()
.resource_table
.take::<FetchRequestResource>(rid)
.map_err(FetchError::Resource)?;
.take::<FetchRequestResource>(rid)?;
let request = Rc::try_unwrap(request)
.ok()
@ -804,9 +816,7 @@ impl Resource for FetchResponseResource {
// safely call `await` on it without creating a race condition.
Some(_) => match reader.as_mut().next().await.unwrap() {
Ok(chunk) => assert!(chunk.is_empty()),
Err(err) => {
break Err(deno_core::error::type_error(err.to_string()))
}
Err(err) => break Err(JsErrorBox::type_error(err.to_string())),
},
None => break Ok(BufView::empty()),
}
@ -814,7 +824,10 @@ impl Resource for FetchResponseResource {
};
let cancel_handle = RcRef::map(self, |r| &r.cancel);
fut.try_or_cancel(cancel_handle).await
fut
.try_or_cancel(cancel_handle)
.await
.map_err(JsErrorBox::from_err)
})
}
@ -897,9 +910,7 @@ where
ca_certs,
proxy: args.proxy,
dns_resolver: if args.use_hickory_resolver {
dns::Resolver::hickory()
.map_err(deno_core::error::AnyError::new)
.map_err(FetchError::Resource)?
dns::Resolver::hickory().map_err(FetchError::Dns)?
} else {
dns::Resolver::default()
},
@ -963,7 +974,8 @@ impl Default for CreateHttpClientOptions {
}
}
#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, deno_error::JsError)]
#[class(type)]
pub enum HttpClientCreateError {
#[error(transparent)]
Tls(deno_tls::TlsError),
@ -973,8 +985,9 @@ pub enum HttpClientCreateError {
InvalidProxyUrl,
#[error("Cannot create Http Client: either `http1` or `http2` needs to be set to true")]
HttpVersionSelectionInvalid,
#[class(inherit)]
#[error(transparent)]
RootCertStore(deno_core::error::AnyError),
RootCertStore(JsErrorBox),
}
/// Create new instance of async Client. This client supports
@ -1097,7 +1110,8 @@ type Connector = proxy::ProxyConnector<HttpConnector<dns::Resolver>>;
#[allow(clippy::declare_interior_mutable_const)]
const STAR_STAR: HeaderValue = HeaderValue::from_static("*/*");
#[derive(Debug)]
#[derive(Debug, deno_error::JsError)]
#[class(type)]
pub struct ClientSendError {
uri: Uri,
pub source: hyper_util::client::legacy::Error,
@ -1172,7 +1186,7 @@ impl Client {
.oneshot(req)
.await
.map_err(|e| ClientSendError { uri, source: e })?;
Ok(resp.map(|b| b.map_err(|e| deno_core::anyhow::anyhow!(e)).boxed()))
Ok(resp.map(|b| b.map_err(|e| JsErrorBox::generic(e.to_string())).boxed()))
}
}
@ -1180,10 +1194,10 @@ impl Client {
pub enum ReqBody {
Full(http_body_util::Full<Bytes>),
Empty(http_body_util::Empty<Bytes>),
Streaming(BoxBody<Bytes, deno_core::error::AnyError>),
Streaming(BoxBody<Bytes, JsErrorBox>),
}
pub type ResBody = BoxBody<Bytes, deno_core::error::AnyError>;
pub type ResBody = BoxBody<Bytes, JsErrorBox>;
impl ReqBody {
pub fn full(bytes: Bytes) -> Self {
@ -1196,7 +1210,7 @@ impl ReqBody {
pub fn streaming<B>(body: B) -> Self
where
B: hyper::body::Body<Data = Bytes, Error = deno_core::error::AnyError>
B: hyper::body::Body<Data = Bytes, Error = JsErrorBox>
+ Send
+ Sync
+ 'static,
@ -1207,7 +1221,7 @@ impl ReqBody {
impl hyper::body::Body for ReqBody {
type Data = Bytes;
type Error = deno_core::error::AnyError;
type Error = JsErrorBox;
fn poll_frame(
mut self: Pin<&mut Self>,