@@ -18,34 +18,22 @@ use thiserror::Error;
1818
1919use crate :: {
2020 bindings:: {
21- pretty_print:: DisplayWithWorld , ReflectAllocationId , ReflectBase , ReflectBaseType ,
22- ReflectReference ,
21+ pretty_print:: { DisplayWithWorld , DisplayWithWorldAndDummy } ,
22+ ReflectAllocationId , ReflectBase , ReflectBaseType , ReflectReference ,
2323 } ,
2424 impl_dummy_display,
2525 prelude:: ScriptValue ,
2626} ;
2727
2828pub type ScriptResult < T > = Result < T , ScriptError > ;
2929
30- #[ derive( Error , Debug ) ]
31- pub struct ScriptErrorWrapper ( ScriptError ) ;
32-
33- impl std:: fmt:: Display for ScriptErrorWrapper {
34- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
35- write ! ( f, "{}" , self . 0 )
36- }
37- }
38-
39- impl From < ScriptError > for Box < dyn std:: error:: Error + Send + Sync + ' static > {
40- fn from ( val : ScriptError ) -> Self {
41- ScriptErrorWrapper ( val) . into ( )
42- }
43- }
4430/// An error with an optional script Context
4531#[ derive( Debug , Clone , PartialEq , Reflect ) ]
4632#[ reflect( opaque) ]
4733pub struct ScriptError ( pub Arc < ScriptErrorInner > ) ;
4834
35+ impl std:: error:: Error for ScriptError { }
36+
4937impl Deref for ScriptError {
5038 type Target = ScriptErrorInner ;
5139
@@ -65,7 +53,7 @@ pub struct ScriptErrorInner {
6553#[ derive( Debug , Clone ) ]
6654pub enum ErrorKind {
6755 Display ( Arc < dyn std:: error:: Error + Send + Sync > ) ,
68- WithWorld ( Arc < dyn DisplayWithWorld + Send + Sync > ) ,
56+ WithWorld ( Arc < dyn DisplayWithWorldAndDummy + Send + Sync > ) ,
6957}
7058
7159impl DisplayWithWorld for ErrorKind {
@@ -81,7 +69,7 @@ impl Display for ErrorKind {
8169 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
8270 match self {
8371 ErrorKind :: Display ( e) => write ! ( f, "{}" , e) ,
84- ErrorKind :: WithWorld ( e) => write ! ( f, "{:? }" , e) ,
72+ ErrorKind :: WithWorld ( e) => write ! ( f, "{}" , e) ,
8573 }
8674 }
8775}
@@ -92,31 +80,26 @@ impl PartialEq for ScriptErrorInner {
9280 }
9381}
9482
95- // impl<T: std::error::Error + Send + Sync + 'static> From<T> for ErrorKind {
96- // fn from(value: T) -> Self {
97- // ErrorKind::Display(Arc::new(value))
98- // }
99- // }
100-
101- // impl<T: DisplayWithWorld + Send + Sync + 'static> From<T> for ErrorKind {
102- // fn from(val: Box<dyn DisplayWithWorld + Send + Sync>) -> Self {
103- // ErrorKind::WithWorld(Arc::from(val))
104- // }
105- // }
106-
10783impl ScriptError {
10884 #[ cfg( feature = "mlua_impls" ) ]
10985 /// Destructures mlua error into a script error, taking care to preserve as much information as possible
11086 pub fn from_mlua_error ( error : mlua:: Error ) -> Self {
11187 match error {
112- mlua:: Error :: ExternalError ( inner) => {
113- if let Some ( script_error) = inner. downcast_ref :: < InteropError > ( ) {
114- script_error. clone ( ) . into ( )
88+ mlua:: Error :: CallbackError { traceback, cause }
89+ if matches ! ( cause. as_ref( ) , mlua:: Error :: ExternalError ( _) ) =>
90+ {
91+ let inner = cause. deref ( ) . clone ( ) ;
92+ Self :: from_mlua_error ( inner) . with_context ( traceback)
93+ }
94+ e => {
95+ if let Some ( inner) = e. downcast_ref :: < InteropError > ( ) {
96+ Self :: new ( inner. clone ( ) )
97+ } else if let Some ( inner) = e. downcast_ref :: < ScriptError > ( ) {
98+ inner. clone ( )
11599 } else {
116- Self :: new_external ( inner )
100+ Self :: new_external ( e )
117101 }
118102 }
119- e => Self :: new_external ( e) ,
120103 }
121104 }
122105
@@ -128,7 +111,7 @@ impl ScriptError {
128111 } ) )
129112 }
130113
131- pub fn new ( reason : impl DisplayWithWorld + Send + Sync + ' static ) -> Self {
114+ pub fn new ( reason : impl DisplayWithWorldAndDummy + Send + Sync + ' static ) -> Self {
132115 Self ( Arc :: new ( ScriptErrorInner {
133116 script : None ,
134117 reason : ErrorKind :: WithWorld ( Arc :: new ( reason) ) ,
@@ -144,6 +127,14 @@ impl ScriptError {
144127 } ) )
145128 }
146129
130+ pub fn with_script < S : ToString > ( self , script : S ) -> Self {
131+ Self ( Arc :: new ( ScriptErrorInner {
132+ script : Some ( script. to_string ( ) ) ,
133+ context : self . 0 . context . clone ( ) ,
134+ reason : self . 0 . reason . clone ( ) ,
135+ } ) )
136+ }
137+
147138 pub fn with_appended_context < S : ToString > ( self , context : S ) -> Self {
148139 Self ( Arc :: new ( ScriptErrorInner {
149140 script : self . 0 . script . clone ( ) ,
@@ -153,26 +144,16 @@ impl ScriptError {
153144 }
154145}
155146
156- // impl<T: std::error::Error + Send + Sync + 'static> From<T> for ScriptError {
157- // fn from(value: T) -> Self {
158- // let error_kind = ErrorKind::from(value);
159- // Self::new_external(error_kind)
160- // }
161- // }
162-
163- // impl<T: DisplayWithWorld + Send + Sync + 'static> From<T> for ScriptError {
164- // fn from(value: T) -> Self {
165- // let error_kind = ErrorKind::WithWorld(Arc::new(value));
166- // Self::new_error(error_kind)
167- // }
168- // }
169-
170147impl std:: fmt:: Display for ScriptError {
171148 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
172149 if let Some ( script) = & self . 0 . script {
173- write ! ( f, "error in script `{}`: {}" , script, self . 0 . reason)
150+ write ! (
151+ f,
152+ "error in script `{}`: {}.\n {}" ,
153+ script, self . 0 . reason, self . 0 . context
154+ )
174155 } else {
175- write ! ( f, "error: {}" , self . 0 . reason)
156+ write ! ( f, "error: {}. \n {} " , self . 0 . reason, self . 0 . context )
176157 }
177158 }
178159}
@@ -181,12 +162,17 @@ impl DisplayWithWorld for ScriptError {
181162 fn display_with_world ( & self , world : crate :: bindings:: WorldGuard ) -> String {
182163 if let Some ( script) = & self . 0 . script {
183164 format ! (
184- "error in script `{}`: {}" ,
165+ "error in script `{}`: {}. \n {} " ,
185166 script,
186- self . 0 . reason. display_with_world( world)
167+ self . 0 . reason. display_with_world( world) ,
168+ self . 0 . context
187169 )
188170 } else {
189- format ! ( "error: {}" , self . 0 . reason. display_with_world( world) )
171+ format ! (
172+ "error: {}.\n {}" ,
173+ self . 0 . reason. display_with_world( world) ,
174+ self . 0 . context
175+ )
190176 }
191177 }
192178}
@@ -350,8 +336,10 @@ impl InteropError {
350336 }
351337}
352338
353- #[ derive( Debug ) ]
339+ impl_dummy_display ! ( InteropErrorInner ) ;
340+
354341/// For errors to do with reflection, type conversions or other interop issues
342+ #[ derive( Debug ) ]
355343pub enum InteropErrorInner {
356344 StaleWorldAccess ,
357345 MissingWorld ,
0 commit comments