@@ -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,21 @@ 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.
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 {
32
+ Self {
33
+ kind : ErrorKind :: Custom ( Some ( user_message. into ( ) ) ) ,
34
+ cause : None ,
35
+ value_str : None ,
36
+ }
37
+ }
27
38
28
39
/// Create a new custom error for a conversion.
29
40
fn custom ( ) -> Self {
30
41
Self {
31
- kind : ErrorKind :: Custom ,
42
+ kind : ErrorKind :: Custom ( None ) ,
32
43
cause : None ,
33
44
value_str : None ,
34
45
}
@@ -52,9 +63,10 @@ impl ConvertError {
52
63
where
53
64
C : Into < Cause > ,
54
65
{
55
- let mut err = Self :: custom ( ) ;
56
- err. cause = Some ( cause. into ( ) ) ;
57
- err
66
+ Self {
67
+ cause : Some ( cause. into ( ) ) ,
68
+ ..Default :: default ( )
69
+ }
58
70
}
59
71
60
72
/// Create a new custom error with a rust-error as an underlying cause for the conversion error, and the
@@ -65,10 +77,11 @@ impl ConvertError {
65
77
C : Into < Cause > ,
66
78
V : fmt:: Debug ,
67
79
{
68
- let mut err = Self :: custom ( ) ;
69
- err. cause = Some ( cause. into ( ) ) ;
70
- err. value_str = Some ( format ! ( "{value:?}" ) ) ;
71
- err
80
+ Self {
81
+ cause : Some ( cause. into ( ) ) ,
82
+ value_str : Some ( format ! ( "{value:?}" ) ) ,
83
+ ..Default :: default ( )
84
+ }
72
85
}
73
86
74
87
/// Returns the rust-error that caused this error, if one exists.
@@ -111,12 +124,21 @@ impl Error for ConvertError {
111
124
}
112
125
}
113
126
127
+ impl Default for ConvertError {
128
+ /// Create a custom error, without any description.
129
+ ///
130
+ /// If you need a custom message, consider using [`ConvertError::new("message")`][Self::new] instead.
131
+ fn default ( ) -> Self {
132
+ Self :: custom ( )
133
+ }
134
+ }
135
+
114
136
#[ derive( Eq , PartialEq , Debug ) ]
115
137
pub ( crate ) enum ErrorKind {
116
138
FromGodot ( FromGodotError ) ,
117
139
FromFfi ( FromFfiError ) ,
118
140
FromVariant ( FromVariantError ) ,
119
- Custom ,
141
+ Custom ( Option < String > ) ,
120
142
}
121
143
122
144
impl ErrorKind {
@@ -125,7 +147,7 @@ impl ErrorKind {
125
147
Self :: FromGodot ( from_godot) => Some ( from_godot. description ( ) ) ,
126
148
Self :: FromVariant ( from_variant) => Some ( from_variant. description ( ) ) ,
127
149
Self :: FromFfi ( from_ffi) => Some ( from_ffi. description ( ) ) ,
128
- Self :: Custom => None ,
150
+ Self :: Custom ( description ) => description . clone ( ) ,
129
151
}
130
152
}
131
153
}
@@ -135,7 +157,7 @@ impl ErrorKind {
135
157
pub ( crate ) enum FromGodotError {
136
158
BadArrayType {
137
159
expected : array_inner:: TypeInfo ,
138
- got : array_inner:: TypeInfo ,
160
+ actual : array_inner:: TypeInfo ,
139
161
} ,
140
162
/// InvalidEnum is also used by bitfields.
141
163
InvalidEnum ,
@@ -152,32 +174,32 @@ impl FromGodotError {
152
174
153
175
fn description ( & self ) -> String {
154
176
match self {
155
- Self :: BadArrayType { expected, got } => {
156
- if expected. variant_type ( ) != got . variant_type ( ) {
157
- if expected. is_typed ( ) {
158
- return format ! (
177
+ Self :: BadArrayType { expected, actual } => {
178
+ if expected. variant_type ( ) != actual . variant_type ( ) {
179
+ return if expected. is_typed ( ) {
180
+ format ! (
159
181
"expected array of type {:?}, got array of type {:?}" ,
160
182
expected. variant_type( ) ,
161
- got . variant_type( )
162
- ) ;
183
+ actual . variant_type( )
184
+ )
163
185
} else {
164
- return format ! (
186
+ format ! (
165
187
"expected untyped array, got array of type {:?}" ,
166
- got . variant_type( )
167
- ) ;
168
- }
188
+ actual . variant_type( )
189
+ )
190
+ } ;
169
191
}
170
192
171
193
assert_ne ! (
172
194
expected. class_name( ) ,
173
- got . class_name( ) ,
195
+ actual . class_name( ) ,
174
196
"BadArrayType with expected == got, this is a gdext bug"
175
197
) ;
176
198
177
199
format ! (
178
200
"expected array of class {}, got array of class {}" ,
179
201
expected. class_name( ) ,
180
- got . class_name( )
202
+ actual . class_name( )
181
203
)
182
204
}
183
205
Self :: InvalidEnum => "invalid engine enum value" . into ( ) ,
0 commit comments