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

refactor(ext/fetch): use concrete error types (#26220)

This commit is contained in:
Leo Kettmeir 2024-10-18 18:20:58 -07:00 committed by GitHub
parent d1cd1fafa4
commit 6c4ef11f04
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 255 additions and 134 deletions

1
Cargo.lock generated
View file

@ -1557,6 +1557,7 @@ dependencies = [
"rustls-webpki", "rustls-webpki",
"serde", "serde",
"serde_json", "serde_json",
"thiserror",
"tokio", "tokio",
"tokio-rustls", "tokio-rustls",
"tokio-socks", "tokio-socks",

View file

@ -32,6 +32,7 @@ percent-encoding.workspace = true
rustls-webpki.workspace = true rustls-webpki.workspace = true
serde.workspace = true serde.workspace = true
serde_json.workspace = true serde_json.workspace = true
thiserror.workspace = true
tokio.workspace = true tokio.workspace = true
tokio-rustls.workspace = true tokio-rustls.workspace = true
tokio-socks.workspace = true tokio-socks.workspace = true

View file

@ -4,7 +4,6 @@ use crate::CancelHandle;
use crate::CancelableResponseFuture; use crate::CancelableResponseFuture;
use crate::FetchHandler; use crate::FetchHandler;
use deno_core::error::type_error;
use deno_core::futures::FutureExt; use deno_core::futures::FutureExt;
use deno_core::futures::TryFutureExt; use deno_core::futures::TryFutureExt;
use deno_core::futures::TryStreamExt; use deno_core::futures::TryStreamExt;
@ -42,9 +41,7 @@ impl FetchHandler for FsFetchHandler {
.map_err(|_| ())?; .map_err(|_| ())?;
Ok::<_, ()>(response) Ok::<_, ()>(response)
} }
.map_err(move |_| { .map_err(move |_| super::FetchError::NetworkError)
type_error("NetworkError when attempting to fetch resource")
})
.or_cancel(&cancel_handle) .or_cancel(&cancel_handle)
.boxed_local(); .boxed_local();

View file

@ -17,10 +17,6 @@ use std::sync::Arc;
use std::task::Context; use std::task::Context;
use std::task::Poll; use std::task::Poll;
use deno_core::anyhow::anyhow;
use deno_core::anyhow::Error;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::futures::stream::Peekable; use deno_core::futures::stream::Peekable;
use deno_core::futures::Future; use deno_core::futures::Future;
use deno_core::futures::FutureExt; use deno_core::futures::FutureExt;
@ -28,6 +24,7 @@ use deno_core::futures::Stream;
use deno_core::futures::StreamExt; use deno_core::futures::StreamExt;
use deno_core::futures::TryFutureExt; use deno_core::futures::TryFutureExt;
use deno_core::op2; use deno_core::op2;
use deno_core::url;
use deno_core::url::Url; use deno_core::url::Url;
use deno_core::AsyncRefCell; use deno_core::AsyncRefCell;
use deno_core::AsyncResult; use deno_core::AsyncResult;
@ -87,15 +84,18 @@ pub struct Options {
pub root_cert_store_provider: Option<Arc<dyn RootCertStoreProvider>>, pub root_cert_store_provider: Option<Arc<dyn RootCertStoreProvider>>,
pub proxy: Option<Proxy>, pub proxy: Option<Proxy>,
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
pub request_builder_hook: pub request_builder_hook: Option<
Option<fn(&mut http::Request<ReqBody>) -> Result<(), AnyError>>, fn(&mut http::Request<ReqBody>) -> Result<(), deno_core::error::AnyError>,
>,
pub unsafely_ignore_certificate_errors: Option<Vec<String>>, pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
pub client_cert_chain_and_key: TlsKeys, pub client_cert_chain_and_key: TlsKeys,
pub file_fetch_handler: Rc<dyn FetchHandler>, pub file_fetch_handler: Rc<dyn FetchHandler>,
} }
impl Options { impl Options {
pub fn root_cert_store(&self) -> Result<Option<RootCertStore>, AnyError> { pub fn root_cert_store(
&self,
) -> Result<Option<RootCertStore>, deno_core::error::AnyError> {
Ok(match &self.root_cert_store_provider { Ok(match &self.root_cert_store_provider {
Some(provider) => Some(provider.get_or_try_init()?.clone()), Some(provider) => Some(provider.get_or_try_init()?.clone()),
None => None, None => None,
@ -144,6 +144,51 @@ deno_core::extension!(deno_fetch,
}, },
); );
#[derive(Debug, thiserror::Error)]
pub enum FetchError {
#[error(transparent)]
Resource(deno_core::error::AnyError),
#[error(transparent)]
Permission(deno_core::error::AnyError),
#[error("NetworkError when attempting to fetch resource")]
NetworkError,
#[error("Fetching files only supports the GET method: received {0}")]
FsNotGet(Method),
#[error("Invalid URL {0}")]
InvalidUrl(Url),
#[error(transparent)]
InvalidHeaderName(#[from] http::header::InvalidHeaderName),
#[error(transparent)]
InvalidHeaderValue(#[from] http::header::InvalidHeaderValue),
#[error("{0:?}")]
DataUrl(data_url::DataUrlError),
#[error("{0:?}")]
Base64(data_url::forgiving_base64::InvalidBase64),
#[error("Blob for the given URL not found.")]
BlobNotFound,
#[error("Url scheme '{0}' not supported")]
SchemeNotSupported(String),
#[error("Request was cancelled")]
RequestCanceled,
#[error(transparent)]
Http(#[from] http::Error),
#[error(transparent)]
ClientCreate(#[from] HttpClientCreateError),
#[error(transparent)]
Url(#[from] url::ParseError),
#[error(transparent)]
Method(#[from] http::method::InvalidMethod),
#[error(transparent)]
ClientSend(#[from] ClientSendError),
#[error(transparent)]
RequestBuilderHook(deno_core::error::AnyError),
#[error(transparent)]
Io(#[from] std::io::Error),
// Only used for node upgrade
#[error(transparent)]
Hyper(#[from] hyper::Error),
}
pub type CancelableResponseFuture = pub type CancelableResponseFuture =
Pin<Box<dyn Future<Output = CancelableResponseResult>>>; Pin<Box<dyn Future<Output = CancelableResponseResult>>>;
@ -170,11 +215,7 @@ impl FetchHandler for DefaultFileFetchHandler {
_state: &mut OpState, _state: &mut OpState,
_url: &Url, _url: &Url,
) -> (CancelableResponseFuture, Option<Rc<CancelHandle>>) { ) -> (CancelableResponseFuture, Option<Rc<CancelHandle>>) {
let fut = async move { let fut = async move { Ok(Err(FetchError::NetworkError)) };
Ok(Err(type_error(
"NetworkError when attempting to fetch resource",
)))
};
(Box::pin(fut), None) (Box::pin(fut), None)
} }
} }
@ -191,7 +232,7 @@ pub struct FetchReturn {
pub fn get_or_create_client_from_state( pub fn get_or_create_client_from_state(
state: &mut OpState, state: &mut OpState,
) -> Result<Client, AnyError> { ) -> Result<Client, HttpClientCreateError> {
if let Some(client) = state.try_borrow::<Client>() { if let Some(client) = state.try_borrow::<Client>() {
Ok(client.clone()) Ok(client.clone())
} else { } else {
@ -204,11 +245,13 @@ pub fn get_or_create_client_from_state(
pub fn create_client_from_options( pub fn create_client_from_options(
options: &Options, options: &Options,
) -> Result<Client, AnyError> { ) -> Result<Client, HttpClientCreateError> {
create_http_client( create_http_client(
&options.user_agent, &options.user_agent,
CreateHttpClientOptions { CreateHttpClientOptions {
root_cert_store: options.root_cert_store()?, root_cert_store: options
.root_cert_store()
.map_err(HttpClientCreateError::RootCertStore)?,
ca_certs: vec![], ca_certs: vec![],
proxy: options.proxy.clone(), proxy: options.proxy.clone(),
unsafely_ignore_certificate_errors: options unsafely_ignore_certificate_errors: options
@ -230,7 +273,9 @@ pub fn create_client_from_options(
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
pub struct ResourceToBodyAdapter( pub struct ResourceToBodyAdapter(
Rc<dyn Resource>, Rc<dyn Resource>,
Option<Pin<Box<dyn Future<Output = Result<BufView, Error>>>>>, Option<
Pin<Box<dyn Future<Output = Result<BufView, deno_core::error::AnyError>>>>,
>,
); );
impl ResourceToBodyAdapter { impl ResourceToBodyAdapter {
@ -246,7 +291,7 @@ unsafe impl Send for ResourceToBodyAdapter {}
unsafe impl Sync for ResourceToBodyAdapter {} unsafe impl Sync for ResourceToBodyAdapter {}
impl Stream for ResourceToBodyAdapter { impl Stream for ResourceToBodyAdapter {
type Item = Result<Bytes, Error>; type Item = Result<Bytes, deno_core::error::AnyError>;
fn poll_next( fn poll_next(
self: Pin<&mut Self>, self: Pin<&mut Self>,
@ -276,7 +321,7 @@ impl Stream for ResourceToBodyAdapter {
impl hyper::body::Body for ResourceToBodyAdapter { impl hyper::body::Body for ResourceToBodyAdapter {
type Data = Bytes; type Data = Bytes;
type Error = Error; type Error = deno_core::error::AnyError;
fn poll_frame( fn poll_frame(
self: Pin<&mut Self>, self: Pin<&mut Self>,
@ -301,13 +346,13 @@ pub trait FetchPermissions {
&mut self, &mut self,
url: &Url, url: &Url,
api_name: &str, api_name: &str,
) -> Result<(), AnyError>; ) -> Result<(), deno_core::error::AnyError>;
#[must_use = "the resolved return value to mitigate time-of-check to time-of-use issues"] #[must_use = "the resolved return value to mitigate time-of-check to time-of-use issues"]
fn check_read<'a>( fn check_read<'a>(
&mut self, &mut self,
p: &'a Path, p: &'a Path,
api_name: &str, api_name: &str,
) -> Result<Cow<'a, Path>, AnyError>; ) -> Result<Cow<'a, Path>, deno_core::error::AnyError>;
} }
impl FetchPermissions for deno_permissions::PermissionsContainer { impl FetchPermissions for deno_permissions::PermissionsContainer {
@ -316,7 +361,7 @@ impl FetchPermissions for deno_permissions::PermissionsContainer {
&mut self, &mut self,
url: &Url, url: &Url,
api_name: &str, api_name: &str,
) -> Result<(), AnyError> { ) -> Result<(), deno_core::error::AnyError> {
deno_permissions::PermissionsContainer::check_net_url(self, url, api_name) deno_permissions::PermissionsContainer::check_net_url(self, url, api_name)
} }
@ -325,7 +370,7 @@ impl FetchPermissions for deno_permissions::PermissionsContainer {
&mut self, &mut self,
path: &'a Path, path: &'a Path,
api_name: &str, api_name: &str,
) -> Result<Cow<'a, Path>, AnyError> { ) -> Result<Cow<'a, Path>, deno_core::error::AnyError> {
deno_permissions::PermissionsContainer::check_read_path( deno_permissions::PermissionsContainer::check_read_path(
self, self,
path, path,
@ -346,12 +391,15 @@ pub fn op_fetch<FP>(
has_body: bool, has_body: bool,
#[buffer] data: Option<JsBuffer>, #[buffer] data: Option<JsBuffer>,
#[smi] resource: Option<ResourceId>, #[smi] resource: Option<ResourceId>,
) -> Result<FetchReturn, AnyError> ) -> Result<FetchReturn, FetchError>
where where
FP: FetchPermissions + 'static, FP: FetchPermissions + 'static,
{ {
let (client, allow_host) = if let Some(rid) = client_rid { let (client, allow_host) = if let Some(rid) = client_rid {
let r = state.resource_table.get::<HttpClientResource>(rid)?; let r = state
.resource_table
.get::<HttpClientResource>(rid)
.map_err(FetchError::Resource)?;
(r.client.clone(), r.allow_host) (r.client.clone(), r.allow_host)
} else { } else {
(get_or_create_client_from_state(state)?, false) (get_or_create_client_from_state(state)?, false)
@ -364,20 +412,18 @@ where
let scheme = url.scheme(); let scheme = url.scheme();
let (request_rid, cancel_handle_rid) = match scheme { let (request_rid, cancel_handle_rid) = match scheme {
"file" => { "file" => {
let path = url.to_file_path().map_err(|_| { let path = url.to_file_path().map_err(|_| FetchError::NetworkError)?;
type_error("NetworkError when attempting to fetch resource")
})?;
let permissions = state.borrow_mut::<FP>(); let permissions = state.borrow_mut::<FP>();
let path = permissions.check_read(&path, "fetch()")?; let path = permissions
.check_read(&path, "fetch()")
.map_err(FetchError::Permission)?;
let url = match path { let url = match path {
Cow::Owned(path) => Url::from_file_path(path).unwrap(), Cow::Owned(path) => Url::from_file_path(path).unwrap(),
Cow::Borrowed(_) => url, Cow::Borrowed(_) => url,
}; };
if method != Method::GET { if method != Method::GET {
return Err(type_error(format!( return Err(FetchError::FsNotGet(method));
"Fetching files only supports the GET method: received {method}"
)));
} }
let Options { let Options {
@ -396,13 +442,15 @@ where
} }
"http" | "https" => { "http" | "https" => {
let permissions = state.borrow_mut::<FP>(); let permissions = state.borrow_mut::<FP>();
permissions.check_net_url(&url, "fetch()")?; permissions
.check_net_url(&url, "fetch()")
.map_err(FetchError::Resource)?;
let maybe_authority = extract_authority(&mut url); let maybe_authority = extract_authority(&mut url);
let uri = url let uri = url
.as_str() .as_str()
.parse::<Uri>() .parse::<Uri>()
.map_err(|_| type_error(format!("Invalid URL {url}")))?; .map_err(|_| FetchError::InvalidUrl(url.clone()))?;
let mut con_len = None; let mut con_len = None;
let body = if has_body { let body = if has_body {
@ -416,7 +464,10 @@ where
.boxed() .boxed()
} }
(_, Some(resource)) => { (_, Some(resource)) => {
let resource = state.resource_table.take_any(resource)?; let resource = state
.resource_table
.take_any(resource)
.map_err(FetchError::Resource)?;
match resource.size_hint() { match resource.size_hint() {
(body_size, Some(n)) if body_size == n && body_size > 0 => { (body_size, Some(n)) if body_size == n && body_size > 0 => {
con_len = Some(body_size); con_len = Some(body_size);
@ -453,10 +504,8 @@ where
} }
for (key, value) in headers { for (key, value) in headers {
let name = HeaderName::from_bytes(&key) let name = HeaderName::from_bytes(&key)?;
.map_err(|err| type_error(err.to_string()))?; let v = HeaderValue::from_bytes(&value)?;
let v = HeaderValue::from_bytes(&value)
.map_err(|err| type_error(err.to_string()))?;
if (name != HOST || allow_host) && name != CONTENT_LENGTH { if (name != HOST || allow_host) && name != CONTENT_LENGTH {
request.headers_mut().append(name, v); request.headers_mut().append(name, v);
@ -474,20 +523,18 @@ where
let options = state.borrow::<Options>(); let options = state.borrow::<Options>();
if let Some(request_builder_hook) = options.request_builder_hook { if let Some(request_builder_hook) = options.request_builder_hook {
request_builder_hook(&mut request) request_builder_hook(&mut request)
.map_err(|err| type_error(err.to_string()))?; .map_err(FetchError::RequestBuilderHook)?;
} }
let cancel_handle = CancelHandle::new_rc(); let cancel_handle = CancelHandle::new_rc();
let cancel_handle_ = cancel_handle.clone(); let cancel_handle_ = cancel_handle.clone();
let fut = { let fut = async move {
async move { client
client .send(request)
.send(request) .map_err(Into::into)
.map_err(Into::into) .or_cancel(cancel_handle_)
.or_cancel(cancel_handle_) .await
.await
}
}; };
let request_rid = state.resource_table.add(FetchRequestResource { let request_rid = state.resource_table.add(FetchRequestResource {
@ -501,12 +548,10 @@ where
(request_rid, Some(cancel_handle_rid)) (request_rid, Some(cancel_handle_rid))
} }
"data" => { "data" => {
let data_url = DataUrl::process(url.as_str()) let data_url =
.map_err(|e| type_error(format!("{e:?}")))?; DataUrl::process(url.as_str()).map_err(FetchError::DataUrl)?;
let (body, _) = data_url let (body, _) = data_url.decode_to_vec().map_err(FetchError::Base64)?;
.decode_to_vec()
.map_err(|e| type_error(format!("{e:?}")))?;
let body = http_body_util::Full::new(body.into()) let body = http_body_util::Full::new(body.into())
.map_err(|never| match never {}) .map_err(|never| match never {})
.boxed(); .boxed();
@ -528,11 +573,9 @@ where
"blob" => { "blob" => {
// Blob URL resolution happens in the JS side of fetch. If we got here is // Blob URL resolution happens in the JS side of fetch. If we got here is
// because the URL isn't an object URL. // because the URL isn't an object URL.
return Err(type_error("Blob for the given URL not found.")); return Err(FetchError::BlobNotFound);
}
_ => {
return Err(type_error(format!("Url scheme '{scheme}' not supported")))
} }
_ => return Err(FetchError::SchemeNotSupported(scheme.to_string())),
}; };
Ok(FetchReturn { Ok(FetchReturn {
@ -564,11 +607,12 @@ pub struct FetchResponse {
pub async fn op_fetch_send( pub async fn op_fetch_send(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
#[smi] rid: ResourceId, #[smi] rid: ResourceId,
) -> Result<FetchResponse, AnyError> { ) -> Result<FetchResponse, FetchError> {
let request = state let request = state
.borrow_mut() .borrow_mut()
.resource_table .resource_table
.take::<FetchRequestResource>(rid)?; .take::<FetchRequestResource>(rid)
.map_err(FetchError::Resource)?;
let request = Rc::try_unwrap(request) let request = Rc::try_unwrap(request)
.ok() .ok()
@ -581,22 +625,23 @@ pub async fn op_fetch_send(
// If any error in the chain is a hyper body error, return that as a special result we can use to // If any error in the chain is a hyper body error, return that as a special result we can use to
// reconstruct an error chain (eg: `new TypeError(..., { cause: new Error(...) })`). // reconstruct an error chain (eg: `new TypeError(..., { cause: new Error(...) })`).
// TODO(mmastrac): it would be a lot easier if we just passed a v8::Global through here instead // TODO(mmastrac): it would be a lot easier if we just passed a v8::Global through here instead
let mut err_ref: &dyn std::error::Error = err.as_ref();
while let Some(err_src) = std::error::Error::source(err_ref) { if let FetchError::ClientSend(err_src) = &err {
if let Some(err_src) = err_src.downcast_ref::<hyper::Error>() { if let Some(client_err) = std::error::Error::source(&err_src.source) {
if let Some(err_src) = std::error::Error::source(err_src) { if let Some(err_src) = client_err.downcast_ref::<hyper::Error>() {
return Ok(FetchResponse { if let Some(err_src) = std::error::Error::source(err_src) {
error: Some((err.to_string(), err_src.to_string())), return Ok(FetchResponse {
..Default::default() error: Some((err.to_string(), err_src.to_string())),
}); ..Default::default()
});
}
} }
} }
err_ref = err_src;
} }
return Err(type_error(err.to_string())); return Err(err);
} }
Err(_) => return Err(type_error("Request was cancelled")), Err(_) => return Err(FetchError::RequestCanceled),
}; };
let status = res.status(); let status = res.status();
@ -636,7 +681,7 @@ pub async fn op_fetch_send(
} }
type CancelableResponseResult = type CancelableResponseResult =
Result<Result<http::Response<ResBody>, AnyError>, Canceled>; Result<Result<http::Response<ResBody>, FetchError>, Canceled>;
pub struct FetchRequestResource { pub struct FetchRequestResource {
pub future: Pin<Box<dyn Future<Output = CancelableResponseResult>>>, pub future: Pin<Box<dyn Future<Output = CancelableResponseResult>>>,
@ -691,7 +736,7 @@ impl FetchResponseResource {
} }
} }
pub async fn upgrade(self) -> Result<hyper::upgrade::Upgraded, AnyError> { pub async fn upgrade(self) -> Result<hyper::upgrade::Upgraded, hyper::Error> {
let reader = self.response_reader.into_inner(); let reader = self.response_reader.into_inner();
match reader { match reader {
FetchResponseReader::Start(resp) => Ok(hyper::upgrade::on(resp).await?), FetchResponseReader::Start(resp) => Ok(hyper::upgrade::on(resp).await?),
@ -746,7 +791,9 @@ impl Resource for FetchResponseResource {
// safely call `await` on it without creating a race condition. // safely call `await` on it without creating a race condition.
Some(_) => match reader.as_mut().next().await.unwrap() { Some(_) => match reader.as_mut().next().await.unwrap() {
Ok(chunk) => assert!(chunk.is_empty()), Ok(chunk) => assert!(chunk.is_empty()),
Err(err) => break Err(type_error(err.to_string())), Err(err) => {
break Err(deno_core::error::type_error(err.to_string()))
}
}, },
None => break Ok(BufView::empty()), None => break Ok(BufView::empty()),
} }
@ -809,14 +856,16 @@ pub fn op_fetch_custom_client<FP>(
state: &mut OpState, state: &mut OpState,
#[serde] args: CreateHttpClientArgs, #[serde] args: CreateHttpClientArgs,
#[cppgc] tls_keys: &TlsKeysHolder, #[cppgc] tls_keys: &TlsKeysHolder,
) -> Result<ResourceId, AnyError> ) -> Result<ResourceId, FetchError>
where where
FP: FetchPermissions + 'static, FP: FetchPermissions + 'static,
{ {
if let Some(proxy) = args.proxy.clone() { if let Some(proxy) = args.proxy.clone() {
let permissions = state.borrow_mut::<FP>(); let permissions = state.borrow_mut::<FP>();
let url = Url::parse(&proxy.url)?; let url = Url::parse(&proxy.url)?;
permissions.check_net_url(&url, "Deno.createHttpClient()")?; permissions
.check_net_url(&url, "Deno.createHttpClient()")
.map_err(FetchError::Permission)?;
} }
let options = state.borrow::<Options>(); let options = state.borrow::<Options>();
@ -829,7 +878,9 @@ where
let client = create_http_client( let client = create_http_client(
&options.user_agent, &options.user_agent,
CreateHttpClientOptions { CreateHttpClientOptions {
root_cert_store: options.root_cert_store()?, root_cert_store: options
.root_cert_store()
.map_err(HttpClientCreateError::RootCertStore)?,
ca_certs, ca_certs,
proxy: args.proxy, proxy: args.proxy,
unsafely_ignore_certificate_errors: options unsafely_ignore_certificate_errors: options
@ -887,19 +938,34 @@ impl Default for CreateHttpClientOptions {
} }
} }
#[derive(Debug, thiserror::Error)]
pub enum HttpClientCreateError {
#[error(transparent)]
Tls(deno_tls::TlsError),
#[error("Illegal characters in User-Agent: received {0}")]
InvalidUserAgent(String),
#[error("invalid proxy url")]
InvalidProxyUrl,
#[error("Cannot create Http Client: either `http1` or `http2` needs to be set to true")]
HttpVersionSelectionInvalid,
#[error(transparent)]
RootCertStore(deno_core::error::AnyError),
}
/// Create new instance of async Client. This client supports /// Create new instance of async Client. This client supports
/// proxies and doesn't follow redirects. /// proxies and doesn't follow redirects.
pub fn create_http_client( pub fn create_http_client(
user_agent: &str, user_agent: &str,
options: CreateHttpClientOptions, options: CreateHttpClientOptions,
) -> Result<Client, AnyError> { ) -> Result<Client, HttpClientCreateError> {
let mut tls_config = deno_tls::create_client_config( let mut tls_config = deno_tls::create_client_config(
options.root_cert_store, options.root_cert_store,
options.ca_certs, options.ca_certs,
options.unsafely_ignore_certificate_errors, options.unsafely_ignore_certificate_errors,
options.client_cert_chain_and_key.into(), options.client_cert_chain_and_key.into(),
deno_tls::SocketUse::Http, deno_tls::SocketUse::Http,
)?; )
.map_err(HttpClientCreateError::Tls)?;
// Proxy TLS should not send ALPN // Proxy TLS should not send ALPN
tls_config.alpn_protocols.clear(); tls_config.alpn_protocols.clear();
@ -919,9 +985,7 @@ pub fn create_http_client(
http_connector.enforce_http(false); http_connector.enforce_http(false);
let user_agent = user_agent.parse::<HeaderValue>().map_err(|_| { let user_agent = user_agent.parse::<HeaderValue>().map_err(|_| {
type_error(format!( HttpClientCreateError::InvalidUserAgent(user_agent.to_string())
"Illegal characters in User-Agent: received {user_agent}"
))
})?; })?;
let mut builder = let mut builder =
@ -932,7 +996,7 @@ pub fn create_http_client(
let mut proxies = proxy::from_env(); let mut proxies = proxy::from_env();
if let Some(proxy) = options.proxy { if let Some(proxy) = options.proxy {
let mut intercept = proxy::Intercept::all(&proxy.url) let mut intercept = proxy::Intercept::all(&proxy.url)
.ok_or_else(|| type_error("invalid proxy url"))?; .ok_or_else(|| HttpClientCreateError::InvalidProxyUrl)?;
if let Some(basic_auth) = &proxy.basic_auth { if let Some(basic_auth) = &proxy.basic_auth {
intercept.set_auth(&basic_auth.username, &basic_auth.password); intercept.set_auth(&basic_auth.username, &basic_auth.password);
} }
@ -964,7 +1028,7 @@ pub fn create_http_client(
} }
(true, true) => {} (true, true) => {}
(false, false) => { (false, false) => {
return Err(type_error("Cannot create Http Client: either `http1` or `http2` needs to be set to true")) return Err(HttpClientCreateError::HttpVersionSelectionInvalid)
} }
} }
@ -980,10 +1044,8 @@ pub fn create_http_client(
#[op2] #[op2]
#[serde] #[serde]
pub fn op_utf8_to_byte_string( pub fn op_utf8_to_byte_string(#[string] input: String) -> ByteString {
#[string] input: String, input.into()
) -> Result<ByteString, AnyError> {
Ok(input.into())
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -1003,7 +1065,7 @@ const STAR_STAR: HeaderValue = HeaderValue::from_static("*/*");
#[derive(Debug)] #[derive(Debug)]
pub struct ClientSendError { pub struct ClientSendError {
uri: Uri, uri: Uri,
source: hyper_util::client::legacy::Error, pub source: hyper_util::client::legacy::Error,
} }
impl ClientSendError { impl ClientSendError {
@ -1075,12 +1137,14 @@ impl Client {
.oneshot(req) .oneshot(req)
.await .await
.map_err(|e| ClientSendError { uri, source: e })?; .map_err(|e| ClientSendError { uri, source: e })?;
Ok(resp.map(|b| b.map_err(|e| anyhow!(e)).boxed())) Ok(resp.map(|b| b.map_err(|e| deno_core::anyhow::anyhow!(e)).boxed()))
} }
} }
pub type ReqBody = http_body_util::combinators::BoxBody<Bytes, Error>; pub type ReqBody =
pub type ResBody = http_body_util::combinators::BoxBody<Bytes, Error>; http_body_util::combinators::BoxBody<Bytes, deno_core::error::AnyError>;
pub type ResBody =
http_body_util::combinators::BoxBody<Bytes, deno_core::error::AnyError>;
/// Copied from https://github.com/seanmonstar/reqwest/blob/b9d62a0323d96f11672a61a17bf8849baec00275/src/async_impl/request.rs#L572 /// Copied from https://github.com/seanmonstar/reqwest/blob/b9d62a0323d96f11672a61a17bf8849baec00275/src/async_impl/request.rs#L572
/// Check the request URL for a "username:password" type authority, and if /// Check the request URL for a "username:password" type authority, and if

View file

@ -8,14 +8,12 @@ use std::task::Context;
use std::task::Poll; use std::task::Poll;
use bytes::Bytes; use bytes::Bytes;
use deno_core::anyhow;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::futures::stream::Peekable; use deno_core::futures::stream::Peekable;
use deno_core::futures::Future; use deno_core::futures::Future;
use deno_core::futures::FutureExt; use deno_core::futures::FutureExt;
use deno_core::futures::Stream; use deno_core::futures::Stream;
use deno_core::futures::StreamExt; use deno_core::futures::StreamExt;
use deno_core::futures::TryFutureExt;
use deno_core::op2; use deno_core::op2;
use deno_core::serde::Serialize; use deno_core::serde::Serialize;
use deno_core::unsync::spawn; use deno_core::unsync::spawn;
@ -33,6 +31,7 @@ use deno_core::Resource;
use deno_core::ResourceId; use deno_core::ResourceId;
use deno_fetch::get_or_create_client_from_state; use deno_fetch::get_or_create_client_from_state;
use deno_fetch::FetchCancelHandle; use deno_fetch::FetchCancelHandle;
use deno_fetch::FetchError;
use deno_fetch::FetchRequestResource; use deno_fetch::FetchRequestResource;
use deno_fetch::FetchReturn; use deno_fetch::FetchReturn;
use deno_fetch::HttpClientResource; use deno_fetch::HttpClientResource;
@ -59,12 +58,15 @@ pub fn op_node_http_request<P>(
#[serde] headers: Vec<(ByteString, ByteString)>, #[serde] headers: Vec<(ByteString, ByteString)>,
#[smi] client_rid: Option<u32>, #[smi] client_rid: Option<u32>,
#[smi] body: Option<ResourceId>, #[smi] body: Option<ResourceId>,
) -> Result<FetchReturn, AnyError> ) -> Result<FetchReturn, FetchError>
where where
P: crate::NodePermissions + 'static, P: crate::NodePermissions + 'static,
{ {
let client = if let Some(rid) = client_rid { let client = if let Some(rid) = client_rid {
let r = state.resource_table.get::<HttpClientResource>(rid)?; let r = state
.resource_table
.get::<HttpClientResource>(rid)
.map_err(FetchError::Resource)?;
r.client.clone() r.client.clone()
} else { } else {
get_or_create_client_from_state(state)? get_or_create_client_from_state(state)?
@ -76,15 +78,15 @@ where
{ {
let permissions = state.borrow_mut::<P>(); let permissions = state.borrow_mut::<P>();
permissions.check_net_url(&url, "ClientRequest")?; permissions
.check_net_url(&url, "ClientRequest")
.map_err(FetchError::Permission)?;
} }
let mut header_map = HeaderMap::new(); let mut header_map = HeaderMap::new();
for (key, value) in headers { for (key, value) in headers {
let name = HeaderName::from_bytes(&key) let name = HeaderName::from_bytes(&key)?;
.map_err(|err| type_error(err.to_string()))?; let v = HeaderValue::from_bytes(&value)?;
let v = HeaderValue::from_bytes(&value)
.map_err(|err| type_error(err.to_string()))?;
header_map.append(name, v); header_map.append(name, v);
} }
@ -92,7 +94,10 @@ where
let (body, con_len) = if let Some(body) = body { let (body, con_len) = if let Some(body) = body {
( (
BodyExt::boxed(NodeHttpResourceToBodyAdapter::new( BodyExt::boxed(NodeHttpResourceToBodyAdapter::new(
state.resource_table.take_any(body)?, state
.resource_table
.take_any(body)
.map_err(FetchError::Resource)?,
)), )),
None, None,
) )
@ -117,7 +122,7 @@ where
*request.uri_mut() = url *request.uri_mut() = url
.as_str() .as_str()
.parse() .parse()
.map_err(|_| type_error("Invalid URL"))?; .map_err(|_| FetchError::InvalidUrl(url.clone()))?;
*request.headers_mut() = header_map; *request.headers_mut() = header_map;
if let Some((username, password)) = maybe_authority { if let Some((username, password)) = maybe_authority {
@ -136,9 +141,9 @@ where
let fut = async move { let fut = async move {
client client
.send(request) .send(request)
.map_err(Into::into)
.or_cancel(cancel_handle_) .or_cancel(cancel_handle_)
.await .await
.map(|res| res.map_err(|err| type_error(err.to_string())))
}; };
let request_rid = state.resource_table.add(FetchRequestResource { let request_rid = state.resource_table.add(FetchRequestResource {
@ -174,11 +179,12 @@ pub struct NodeHttpFetchResponse {
pub async fn op_node_http_fetch_send( pub async fn op_node_http_fetch_send(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
#[smi] rid: ResourceId, #[smi] rid: ResourceId,
) -> Result<NodeHttpFetchResponse, AnyError> { ) -> Result<NodeHttpFetchResponse, FetchError> {
let request = state let request = state
.borrow_mut() .borrow_mut()
.resource_table .resource_table
.take::<FetchRequestResource>(rid)?; .take::<FetchRequestResource>(rid)
.map_err(FetchError::Resource)?;
let request = Rc::try_unwrap(request) let request = Rc::try_unwrap(request)
.ok() .ok()
@ -191,22 +197,23 @@ pub async fn op_node_http_fetch_send(
// If any error in the chain is a hyper body error, return that as a special result we can use to // If any error in the chain is a hyper body error, return that as a special result we can use to
// reconstruct an error chain (eg: `new TypeError(..., { cause: new Error(...) })`). // reconstruct an error chain (eg: `new TypeError(..., { cause: new Error(...) })`).
// TODO(mmastrac): it would be a lot easier if we just passed a v8::Global through here instead // TODO(mmastrac): it would be a lot easier if we just passed a v8::Global through here instead
let mut err_ref: &dyn std::error::Error = err.as_ref();
while let Some(err) = std::error::Error::source(err_ref) { if let FetchError::ClientSend(err_src) = &err {
if let Some(err) = err.downcast_ref::<hyper::Error>() { if let Some(client_err) = std::error::Error::source(&err_src.source) {
if let Some(err) = std::error::Error::source(err) { if let Some(err_src) = client_err.downcast_ref::<hyper::Error>() {
return Ok(NodeHttpFetchResponse { if let Some(err_src) = std::error::Error::source(err_src) {
error: Some(err.to_string()), return Ok(NodeHttpFetchResponse {
..Default::default() error: Some(err_src.to_string()),
}); ..Default::default()
});
}
} }
} }
err_ref = err;
} }
return Err(type_error(err.to_string())); return Err(err);
} }
Err(_) => return Err(type_error("request was cancelled")), Err(_) => return Err(FetchError::RequestCanceled),
}; };
let status = res.status(); let status = res.status();
@ -250,11 +257,12 @@ pub async fn op_node_http_fetch_send(
pub async fn op_node_http_fetch_response_upgrade( pub async fn op_node_http_fetch_response_upgrade(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
#[smi] rid: ResourceId, #[smi] rid: ResourceId,
) -> Result<ResourceId, AnyError> { ) -> Result<ResourceId, FetchError> {
let raw_response = state let raw_response = state
.borrow_mut() .borrow_mut()
.resource_table .resource_table
.take::<NodeHttpFetchResponseResource>(rid)?; .take::<NodeHttpFetchResponseResource>(rid)
.map_err(FetchError::Resource)?;
let raw_response = Rc::try_unwrap(raw_response) let raw_response = Rc::try_unwrap(raw_response)
.expect("Someone is holding onto NodeHttpFetchResponseResource"); .expect("Someone is holding onto NodeHttpFetchResponseResource");
@ -277,7 +285,7 @@ pub async fn op_node_http_fetch_response_upgrade(
} }
read_tx.write_all(&buf[..read]).await?; read_tx.write_all(&buf[..read]).await?;
} }
Ok::<_, AnyError>(()) Ok::<_, FetchError>(())
}); });
spawn(async move { spawn(async move {
let mut buf = [0; 1024]; let mut buf = [0; 1024];
@ -288,7 +296,7 @@ pub async fn op_node_http_fetch_response_upgrade(
} }
upgraded_tx.write_all(&buf[..read]).await?; upgraded_tx.write_all(&buf[..read]).await?;
} }
Ok::<_, AnyError>(()) Ok::<_, FetchError>(())
}); });
} }
@ -318,23 +326,26 @@ impl UpgradeStream {
} }
} }
async fn read(self: Rc<Self>, buf: &mut [u8]) -> Result<usize, AnyError> { async fn read(
self: Rc<Self>,
buf: &mut [u8],
) -> Result<usize, std::io::Error> {
let cancel_handle = RcRef::map(self.clone(), |this| &this.cancel_handle); let cancel_handle = RcRef::map(self.clone(), |this| &this.cancel_handle);
async { async {
let read = RcRef::map(self, |this| &this.read); let read = RcRef::map(self, |this| &this.read);
let mut read = read.borrow_mut().await; let mut read = read.borrow_mut().await;
Ok(Pin::new(&mut *read).read(buf).await?) Pin::new(&mut *read).read(buf).await
} }
.try_or_cancel(cancel_handle) .try_or_cancel(cancel_handle)
.await .await
} }
async fn write(self: Rc<Self>, buf: &[u8]) -> Result<usize, AnyError> { async fn write(self: Rc<Self>, buf: &[u8]) -> Result<usize, std::io::Error> {
let cancel_handle = RcRef::map(self.clone(), |this| &this.cancel_handle); let cancel_handle = RcRef::map(self.clone(), |this| &this.cancel_handle);
async { async {
let write = RcRef::map(self, |this| &this.write); let write = RcRef::map(self, |this| &this.write);
let mut write = write.borrow_mut().await; let mut write = write.borrow_mut().await;
Ok(Pin::new(&mut *write).write(buf).await?) Pin::new(&mut *write).write(buf).await
} }
.try_or_cancel(cancel_handle) .try_or_cancel(cancel_handle)
.await .await
@ -387,7 +398,7 @@ impl NodeHttpFetchResponseResource {
} }
} }
pub async fn upgrade(self) -> Result<hyper::upgrade::Upgraded, AnyError> { pub async fn upgrade(self) -> Result<hyper::upgrade::Upgraded, hyper::Error> {
let reader = self.response_reader.into_inner(); let reader = self.response_reader.into_inner();
match reader { match reader {
NodeHttpFetchResponseReader::Start(resp) => { NodeHttpFetchResponseReader::Start(resp) => {
@ -445,7 +456,9 @@ impl Resource for NodeHttpFetchResponseResource {
// safely call `await` on it without creating a race condition. // safely call `await` on it without creating a race condition.
Some(_) => match reader.as_mut().next().await.unwrap() { Some(_) => match reader.as_mut().next().await.unwrap() {
Ok(chunk) => assert!(chunk.is_empty()), Ok(chunk) => assert!(chunk.is_empty()),
Err(err) => break Err(type_error(err.to_string())), Err(err) => {
break Err(deno_core::error::type_error(err.to_string()))
}
}, },
None => break Ok(BufView::empty()), None => break Ok(BufView::empty()),
} }
@ -453,7 +466,7 @@ impl Resource for NodeHttpFetchResponseResource {
}; };
let cancel_handle = RcRef::map(self, |r| &r.cancel); 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(Into::into)
}) })
} }
@ -469,7 +482,9 @@ impl Resource for NodeHttpFetchResponseResource {
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
pub struct NodeHttpResourceToBodyAdapter( pub struct NodeHttpResourceToBodyAdapter(
Rc<dyn Resource>, Rc<dyn Resource>,
Option<Pin<Box<dyn Future<Output = Result<BufView, anyhow::Error>>>>>, Option<
Pin<Box<dyn Future<Output = Result<BufView, deno_core::anyhow::Error>>>>,
>,
); );
impl NodeHttpResourceToBodyAdapter { impl NodeHttpResourceToBodyAdapter {
@ -485,7 +500,7 @@ unsafe impl Send for NodeHttpResourceToBodyAdapter {}
unsafe impl Sync for NodeHttpResourceToBodyAdapter {} unsafe impl Sync for NodeHttpResourceToBodyAdapter {}
impl Stream for NodeHttpResourceToBodyAdapter { impl Stream for NodeHttpResourceToBodyAdapter {
type Item = Result<Bytes, anyhow::Error>; type Item = Result<Bytes, deno_core::anyhow::Error>;
fn poll_next( fn poll_next(
self: Pin<&mut Self>, self: Pin<&mut Self>,
@ -515,7 +530,7 @@ impl Stream for NodeHttpResourceToBodyAdapter {
impl hyper::body::Body for NodeHttpResourceToBodyAdapter { impl hyper::body::Body for NodeHttpResourceToBodyAdapter {
type Data = Bytes; type Data = Bytes;
type Error = anyhow::Error; type Error = deno_core::anyhow::Error;
fn poll_frame( fn poll_frame(
self: Pin<&mut Self>, self: Pin<&mut Self>,

View file

@ -22,6 +22,8 @@ use deno_crypto::EncryptError;
use deno_crypto::ExportKeyError; use deno_crypto::ExportKeyError;
use deno_crypto::GenerateKeyError; use deno_crypto::GenerateKeyError;
use deno_crypto::ImportKeyError; use deno_crypto::ImportKeyError;
use deno_fetch::FetchError;
use deno_fetch::HttpClientCreateError;
use deno_ffi::CallError; use deno_ffi::CallError;
use deno_ffi::CallbackError; use deno_ffi::CallbackError;
use deno_ffi::DlfcnError; use deno_ffi::DlfcnError;
@ -537,6 +539,42 @@ fn get_broadcast_channel_error(error: &BroadcastChannelError) -> &'static str {
} }
} }
fn get_fetch_error(error: &FetchError) -> &'static str {
match error {
FetchError::Resource(e) | FetchError::Permission(e) => {
get_error_class_name(e).unwrap_or("Error")
}
FetchError::NetworkError => "TypeError",
FetchError::FsNotGet(_) => "TypeError",
FetchError::InvalidUrl(_) => "TypeError",
FetchError::InvalidHeaderName(_) => "TypeError",
FetchError::InvalidHeaderValue(_) => "TypeError",
FetchError::DataUrl(_) => "TypeError",
FetchError::Base64(_) => "TypeError",
FetchError::BlobNotFound => "TypeError",
FetchError::SchemeNotSupported(_) => "TypeError",
FetchError::RequestCanceled => "TypeError",
FetchError::Http(_) => "Error",
FetchError::ClientCreate(e) => get_http_client_create_error(e),
FetchError::Url(e) => get_url_parse_error_class(e),
FetchError::Method(_) => "TypeError",
FetchError::ClientSend(_) => "TypeError",
FetchError::RequestBuilderHook(_) => "TypeError",
FetchError::Io(e) => get_io_error_class(e),
FetchError::Hyper(e) => get_hyper_error_class(e),
}
}
fn get_http_client_create_error(error: &HttpClientCreateError) -> &'static str {
match error {
HttpClientCreateError::Tls(_) => "TypeError",
HttpClientCreateError::InvalidUserAgent(_) => "TypeError",
HttpClientCreateError::InvalidProxyUrl => "TypeError",
HttpClientCreateError::HttpVersionSelectionInvalid => "TypeError",
HttpClientCreateError::RootCertStore(_) => "TypeError",
}
}
fn get_websocket_error(error: &WebsocketError) -> &'static str { fn get_websocket_error(error: &WebsocketError) -> &'static str {
match error { match error {
WebsocketError::Permission(e) | WebsocketError::Resource(e) => { WebsocketError::Permission(e) | WebsocketError::Resource(e) => {
@ -788,6 +826,11 @@ pub fn get_error_class_name(e: &AnyError) -> Option<&'static str> {
.map(get_websocket_handshake_error) .map(get_websocket_handshake_error)
}) })
.or_else(|| e.downcast_ref::<KvError>().map(get_kv_error)) .or_else(|| e.downcast_ref::<KvError>().map(get_kv_error))
.or_else(|| e.downcast_ref::<FetchError>().map(get_fetch_error))
.or_else(|| {
e.downcast_ref::<HttpClientCreateError>()
.map(get_http_client_create_error)
})
.or_else(|| e.downcast_ref::<NetError>().map(get_net_error)) .or_else(|| e.downcast_ref::<NetError>().map(get_net_error))
.or_else(|| { .or_else(|| {
e.downcast_ref::<deno_net::io::MapError>() e.downcast_ref::<deno_net::io::MapError>()