@@ -217,7 +217,7 @@ pub(crate) struct ExceptionThrown {
217217
218218impl ExceptionThrown {
219219 /// Turns a caught JS exception in `scope` into a [`JSError`].
220- pub ( crate ) fn into_error ( self , scope : & mut PinTryCatch ) -> JsError {
220+ pub ( crate ) fn into_error ( self , scope : & mut PinTryCatch ) -> Result < JsError , UnknownJsError > {
221221 JsError :: from_caught ( scope)
222222 }
223223}
@@ -254,12 +254,15 @@ pub(super) enum ErrorOrException<Exc> {
254254 Exception ( Exc ) ,
255255}
256256
257- impl < Exc > ErrorOrException < Exc > {
258- pub ( super ) fn map_exception < Exc2 > ( self , f : impl FnOnce ( Exc ) -> Exc2 ) -> ErrorOrException < Exc2 > {
259- match self {
257+ impl ErrorOrException < ExceptionThrown > {
258+ pub ( super ) fn exc_into_error (
259+ self ,
260+ scope : & mut PinTryCatch < ' _ , ' _ , ' _ , ' _ > ,
261+ ) -> Result < ErrorOrException < JsError > , UnknownJsError > {
262+ Ok ( match self {
260263 ErrorOrException :: Err ( e) => ErrorOrException :: Err ( e) ,
261- ErrorOrException :: Exception ( exc) => ErrorOrException :: Exception ( f ( exc) ) ,
262- }
264+ ErrorOrException :: Exception ( exc) => ErrorOrException :: Exception ( exc. into_error ( scope ) ? ) ,
265+ } )
263266 }
264267}
265268
@@ -275,6 +278,12 @@ impl From<ExceptionThrown> for ErrorOrException<ExceptionThrown> {
275278 }
276279}
277280
281+ impl From < JsError > for ErrorOrException < JsError > {
282+ fn from ( e : JsError ) -> Self {
283+ Self :: Exception ( e)
284+ }
285+ }
286+
278287impl From < ErrorOrException < JsError > > for anyhow:: Error {
279288 fn from ( err : ErrorOrException < JsError > ) -> Self {
280289 match err {
@@ -528,23 +537,41 @@ fn get_or_insert_slot<T: 'static>(isolate: &mut v8::Isolate, default: impl FnOnc
528537
529538impl JsError {
530539 /// Turns a caught JS exception in `scope` into a [`JSError`].
531- fn from_caught ( scope : & mut PinTryCatch < ' _ , ' _ , ' _ , ' _ > ) -> Self {
532- match scope. message ( ) {
533- Some ( message) => Self {
534- trace : message
535- . get_stack_trace ( scope)
536- . map ( |trace| JsStackTrace :: from_trace ( scope, trace) )
537- . unwrap_or_default ( ) ,
538- msg : message. get ( scope) . to_rust_string_lossy ( scope) ,
539- } ,
540- None => Self {
541- trace : JsStackTrace :: default ( ) ,
542- msg : "unknown error" . to_owned ( ) ,
543- } ,
540+ fn from_caught ( scope : & mut PinTryCatch < ' _ , ' _ , ' _ , ' _ > ) -> Result < Self , UnknownJsError > {
541+ let message = scope. message ( ) . ok_or ( UnknownJsError ) ?;
542+ Ok ( Self {
543+ trace : message
544+ . get_stack_trace ( scope)
545+ . map ( |trace| JsStackTrace :: from_trace ( scope, trace) )
546+ . unwrap_or_default ( ) ,
547+ msg : message. get ( scope) . to_rust_string_lossy ( scope) ,
548+ } )
549+ }
550+ }
551+
552+ pub ( super ) struct UnknownJsError ;
553+
554+ impl From < UnknownJsError > for JsError {
555+ fn from ( _: UnknownJsError ) -> Self {
556+ Self {
557+ trace : JsStackTrace :: default ( ) ,
558+ msg : "unknown error" . to_owned ( ) ,
544559 }
545560 }
546561}
547562
563+ impl From < UnknownJsError > for ErrorOrException < JsError > {
564+ fn from ( e : UnknownJsError ) -> Self {
565+ Self :: Exception ( e. into ( ) )
566+ }
567+ }
568+
569+ impl From < UnknownJsError > for anyhow:: Error {
570+ fn from ( e : UnknownJsError ) -> Self {
571+ JsError :: from ( e) . into ( )
572+ }
573+ }
574+
548575pub ( super ) fn log_traceback ( replica_ctx : & ReplicaContext , func_type : & str , func : & str , e : & anyhow:: Error ) {
549576 log:: info!( "{func_type} \" {func}\" runtime error: {e:}" ) ;
550577 if let Some ( js_err) = e. downcast_ref :: < JsError > ( ) {
@@ -573,7 +600,7 @@ pub(super) fn catch_exception<'scope, T>(
573600 body : impl FnOnce ( & mut PinTryCatch < ' scope , ' _ , ' _ , ' _ > ) -> Result < T , ErrorOrException < ExceptionThrown > > ,
574601) -> Result < T , ErrorOrException < JsError > > {
575602 tc_scope ! ( scope, scope) ;
576- body ( scope) . map_err ( |e| e. map_exception ( |exc| exc . into_error ( scope) ) )
603+ body ( scope) . map_err ( |e| e. exc_into_error ( scope) . unwrap_or_else ( Into :: into ) )
577604}
578605
579606pub ( super ) type PinTryCatch < ' scope , ' iso , ' x , ' s > = PinnedRef < ' x , TryCatch < ' s , ' scope , HandleScope < ' iso > > > ;
0 commit comments