From 01a6b66034b53dbeffaa12d1d408066a1bc28643 Mon Sep 17 00:00:00 2001 From: Andreu Botella Date: Thu, 16 Dec 2021 12:57:26 +0100 Subject: feat: support abort reasons in Deno APIs and `WebSocketStream` (#13066) --- ext/websocket/02_websocketstream.js | 12 +++++++----- ext/websocket/lib.rs | 31 +------------------------------ 2 files changed, 8 insertions(+), 35 deletions(-) (limited to 'ext') diff --git a/ext/websocket/02_websocketstream.js b/ext/websocket/02_websocketstream.js index 246ddb1e3..dba22d557 100644 --- a/ext/websocket/02_websocketstream.js +++ b/ext/websocket/02_websocketstream.js @@ -125,10 +125,7 @@ if (options.signal?.aborted) { core.close(cancelRid); - const err = new DOMException( - "This operation was aborted", - "AbortError", - ); + const err = options.signal.reason; this[_connection].reject(err); this[_closed].reject(err); } else { @@ -313,7 +310,12 @@ } }, (err) => { - core.tryClose(cancelRid); + if (err instanceof core.Interrupted) { + // The signal was aborted. + err = options.signal.reason; + } else { + core.tryClose(cancelRid); + } this[_connection].reject(err); this[_closed].reject(err); }, diff --git a/ext/websocket/lib.rs b/ext/websocket/lib.rs index 13d3ddfca..4796eddc6 100644 --- a/ext/websocket/lib.rs +++ b/ext/websocket/lib.rs @@ -298,10 +298,7 @@ where let client = client_async(request, socket); let (stream, response): (WsStream, Response) = if let Some(cancel_resource) = cancel_resource { - client - .or_cancel(cancel_resource.0.to_owned()) - .await - .map_err(|_| DomExceptionAbortError::new("connection was aborted"))? + client.or_cancel(cancel_resource.0.to_owned()).await? } else { client.await } @@ -508,29 +505,3 @@ pub fn get_network_error_class_name(e: &AnyError) -> Option<&'static str> { e.downcast_ref::() .map(|_| "DOMExceptionNetworkError") } - -#[derive(Debug)] -pub struct DomExceptionAbortError { - pub msg: String, -} - -impl DomExceptionAbortError { - pub fn new(msg: &str) -> Self { - DomExceptionAbortError { - msg: msg.to_string(), - } - } -} - -impl fmt::Display for DomExceptionAbortError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.pad(&self.msg) - } -} - -impl std::error::Error for DomExceptionAbortError {} - -pub fn get_abort_error_class_name(e: &AnyError) -> Option<&'static str> { - e.downcast_ref::() - .map(|_| "DOMExceptionAbortError") -} -- cgit v1.2.3