@@ -18,34 +18,22 @@ use thiserror::Error;
18
18
19
19
use crate :: {
20
20
bindings:: {
21
- pretty_print:: DisplayWithWorld , ReflectAllocationId , ReflectBase , ReflectBaseType ,
22
- ReflectReference ,
21
+ pretty_print:: { DisplayWithWorld , DisplayWithWorldAndDummy } ,
22
+ ReflectAllocationId , ReflectBase , ReflectBaseType , ReflectReference ,
23
23
} ,
24
24
impl_dummy_display,
25
25
prelude:: ScriptValue ,
26
26
} ;
27
27
28
28
pub type ScriptResult < T > = Result < T , ScriptError > ;
29
29
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
- }
44
30
/// An error with an optional script Context
45
31
#[ derive( Debug , Clone , PartialEq , Reflect ) ]
46
32
#[ reflect( opaque) ]
47
33
pub struct ScriptError ( pub Arc < ScriptErrorInner > ) ;
48
34
35
+ impl std:: error:: Error for ScriptError { }
36
+
49
37
impl Deref for ScriptError {
50
38
type Target = ScriptErrorInner ;
51
39
@@ -65,7 +53,7 @@ pub struct ScriptErrorInner {
65
53
#[ derive( Debug , Clone ) ]
66
54
pub enum ErrorKind {
67
55
Display ( Arc < dyn std:: error:: Error + Send + Sync > ) ,
68
- WithWorld ( Arc < dyn DisplayWithWorld + Send + Sync > ) ,
56
+ WithWorld ( Arc < dyn DisplayWithWorldAndDummy + Send + Sync > ) ,
69
57
}
70
58
71
59
impl DisplayWithWorld for ErrorKind {
@@ -81,7 +69,7 @@ impl Display for ErrorKind {
81
69
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
82
70
match self {
83
71
ErrorKind :: Display ( e) => write ! ( f, "{}" , e) ,
84
- ErrorKind :: WithWorld ( e) => write ! ( f, "{:? }" , e) ,
72
+ ErrorKind :: WithWorld ( e) => write ! ( f, "{}" , e) ,
85
73
}
86
74
}
87
75
}
@@ -92,31 +80,26 @@ impl PartialEq for ScriptErrorInner {
92
80
}
93
81
}
94
82
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
-
107
83
impl ScriptError {
108
84
#[ cfg( feature = "mlua_impls" ) ]
109
85
/// Destructures mlua error into a script error, taking care to preserve as much information as possible
110
86
pub fn from_mlua_error ( error : mlua:: Error ) -> Self {
111
87
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 ( )
115
99
} else {
116
- Self :: new_external ( inner )
100
+ Self :: new_external ( e )
117
101
}
118
102
}
119
- e => Self :: new_external ( e) ,
120
103
}
121
104
}
122
105
@@ -128,7 +111,7 @@ impl ScriptError {
128
111
} ) )
129
112
}
130
113
131
- pub fn new ( reason : impl DisplayWithWorld + Send + Sync + ' static ) -> Self {
114
+ pub fn new ( reason : impl DisplayWithWorldAndDummy + Send + Sync + ' static ) -> Self {
132
115
Self ( Arc :: new ( ScriptErrorInner {
133
116
script : None ,
134
117
reason : ErrorKind :: WithWorld ( Arc :: new ( reason) ) ,
@@ -144,6 +127,14 @@ impl ScriptError {
144
127
} ) )
145
128
}
146
129
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
+
147
138
pub fn with_appended_context < S : ToString > ( self , context : S ) -> Self {
148
139
Self ( Arc :: new ( ScriptErrorInner {
149
140
script : self . 0 . script . clone ( ) ,
@@ -153,26 +144,16 @@ impl ScriptError {
153
144
}
154
145
}
155
146
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
-
170
147
impl std:: fmt:: Display for ScriptError {
171
148
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
172
149
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
+ )
174
155
} else {
175
- write ! ( f, "error: {}" , self . 0 . reason)
156
+ write ! ( f, "error: {}. \n {} " , self . 0 . reason, self . 0 . context )
176
157
}
177
158
}
178
159
}
@@ -181,12 +162,17 @@ impl DisplayWithWorld for ScriptError {
181
162
fn display_with_world ( & self , world : crate :: bindings:: WorldGuard ) -> String {
182
163
if let Some ( script) = & self . 0 . script {
183
164
format ! (
184
- "error in script `{}`: {}" ,
165
+ "error in script `{}`: {}. \n {} " ,
185
166
script,
186
- self . 0 . reason. display_with_world( world)
167
+ self . 0 . reason. display_with_world( world) ,
168
+ self . 0 . context
187
169
)
188
170
} 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
+ )
190
176
}
191
177
}
192
178
}
@@ -350,8 +336,10 @@ impl InteropError {
350
336
}
351
337
}
352
338
353
- #[ derive( Debug ) ]
339
+ impl_dummy_display ! ( InteropErrorInner ) ;
340
+
354
341
/// For errors to do with reflection, type conversions or other interop issues
342
+ #[ derive( Debug ) ]
355
343
pub enum InteropErrorInner {
356
344
StaleWorldAccess ,
357
345
MissingWorld ,
0 commit comments