@@ -15,6 +15,8 @@ use crate::builtin::{array_inner, meta::ClassName};
15
15
type Cause = Box < dyn Error + Send + Sync > ;
16
16
17
17
/// Represents errors that can occur when converting values from Godot.
18
+ ///
19
+ /// To create user-defined errors, you can use [`ConvertError::default()`] or [`ConvertError::new("message")`][Self::new].
18
20
#[ derive( Debug ) ]
19
21
pub struct ConvertError {
20
22
kind : ErrorKind ,
@@ -23,12 +25,12 @@ pub struct ConvertError {
23
25
}
24
26
25
27
impl ConvertError {
26
- // Constructors are private (or hidden) as only the library or its proc-macros should construct this type .
27
-
28
- /// Create a new custom error for a conversion .
29
- fn custom ( ) -> Self {
28
+ /// Construct with a user-defined message .
29
+ ///
30
+ /// If you don't need a custom message, consider using [`ConvertError::default()`] instead .
31
+ pub fn new ( user_message : impl Into < String > ) -> Self {
30
32
Self {
31
- kind : ErrorKind :: Custom ,
33
+ kind : ErrorKind :: Custom ( Some ( user_message . into ( ) ) ) ,
32
34
cause : None ,
33
35
value_str : None ,
34
36
}
@@ -52,9 +54,10 @@ impl ConvertError {
52
54
where
53
55
C : Into < Cause > ,
54
56
{
55
- let mut err = Self :: custom ( ) ;
56
- err. cause = Some ( cause. into ( ) ) ;
57
- err
57
+ Self {
58
+ cause : Some ( cause. into ( ) ) ,
59
+ ..Default :: default ( )
60
+ }
58
61
}
59
62
60
63
/// Create a new custom error with a rust-error as an underlying cause for the conversion error, and the
@@ -65,10 +68,11 @@ impl ConvertError {
65
68
C : Into < Cause > ,
66
69
V : fmt:: Debug ,
67
70
{
68
- let mut err = Self :: custom ( ) ;
69
- err. cause = Some ( cause. into ( ) ) ;
70
- err. value_str = Some ( format ! ( "{value:?}" ) ) ;
71
- err
71
+ Self {
72
+ cause : Some ( cause. into ( ) ) ,
73
+ value_str : Some ( format ! ( "{value:?}" ) ) ,
74
+ ..Default :: default ( )
75
+ }
72
76
}
73
77
74
78
/// Returns the rust-error that caused this error, if one exists.
@@ -111,12 +115,25 @@ impl Error for ConvertError {
111
115
}
112
116
}
113
117
118
+ impl Default for ConvertError {
119
+ /// Create a custom error, without any description.
120
+ ///
121
+ /// If you need a custom message, consider using [`ConvertError::new("message")`][Self::new] instead.
122
+ fn default ( ) -> Self {
123
+ Self {
124
+ kind : ErrorKind :: Custom ( None ) ,
125
+ cause : None ,
126
+ value_str : None ,
127
+ }
128
+ }
129
+ }
130
+
114
131
#[ derive( Eq , PartialEq , Debug ) ]
115
132
pub ( crate ) enum ErrorKind {
116
133
FromGodot ( FromGodotError ) ,
117
134
FromFfi ( FromFfiError ) ,
118
135
FromVariant ( FromVariantError ) ,
119
- Custom ,
136
+ Custom ( Option < String > ) ,
120
137
}
121
138
122
139
impl ErrorKind {
@@ -125,7 +142,7 @@ impl ErrorKind {
125
142
Self :: FromGodot ( from_godot) => Some ( from_godot. description ( ) ) ,
126
143
Self :: FromVariant ( from_variant) => Some ( from_variant. description ( ) ) ,
127
144
Self :: FromFfi ( from_ffi) => Some ( from_ffi. description ( ) ) ,
128
- Self :: Custom => None ,
145
+ Self :: Custom ( description ) => description . clone ( ) ,
129
146
}
130
147
}
131
148
}
@@ -135,7 +152,7 @@ impl ErrorKind {
135
152
pub ( crate ) enum FromGodotError {
136
153
BadArrayType {
137
154
expected : array_inner:: TypeInfo ,
138
- got : array_inner:: TypeInfo ,
155
+ actual : array_inner:: TypeInfo ,
139
156
} ,
140
157
/// InvalidEnum is also used by bitfields.
141
158
InvalidEnum ,
@@ -152,32 +169,32 @@ impl FromGodotError {
152
169
153
170
fn description ( & self ) -> String {
154
171
match self {
155
- Self :: BadArrayType { expected, got } => {
156
- if expected. variant_type ( ) != got . variant_type ( ) {
157
- if expected. is_typed ( ) {
158
- return format ! (
172
+ Self :: BadArrayType { expected, actual } => {
173
+ if expected. variant_type ( ) != actual . variant_type ( ) {
174
+ return if expected. is_typed ( ) {
175
+ format ! (
159
176
"expected array of type {:?}, got array of type {:?}" ,
160
177
expected. variant_type( ) ,
161
- got . variant_type( )
162
- ) ;
178
+ actual . variant_type( )
179
+ )
163
180
} else {
164
- return format ! (
181
+ format ! (
165
182
"expected untyped array, got array of type {:?}" ,
166
- got . variant_type( )
167
- ) ;
168
- }
183
+ actual . variant_type( )
184
+ )
185
+ } ;
169
186
}
170
187
171
188
assert_ne ! (
172
189
expected. class_name( ) ,
173
- got . class_name( ) ,
190
+ actual . class_name( ) ,
174
191
"BadArrayType with expected == got, this is a gdext bug"
175
192
) ;
176
193
177
194
format ! (
178
195
"expected array of class {}, got array of class {}" ,
179
196
expected. class_name( ) ,
180
- got . class_name( )
197
+ actual . class_name( )
181
198
)
182
199
}
183
200
Self :: InvalidEnum => "invalid engine enum value" . into ( ) ,
0 commit comments