Skip to content

Commit fe5b02b

Browse files
authored
Merge pull request #645 from godot-rust/bugfix/convert-error-ctor
`ConvertError`: add back default, new constructor with message
2 parents 15abee1 + 8544686 commit fe5b02b

File tree

2 files changed

+45
-28
lines changed

2 files changed

+45
-28
lines changed

godot-core/src/builtin/array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ impl<T: GodotType> Array<T> {
434434
} else {
435435
Err(FromGodotError::BadArrayType {
436436
expected: target_ty,
437-
got: self_ty,
437+
actual: self_ty,
438438
}
439439
.into_error(self))
440440
}

godot-core/src/builtin/meta/godot_convert/convert_error.rs

+44-27
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use crate::builtin::{array_inner, meta::ClassName};
1515
type Cause = Box<dyn Error + Send + Sync>;
1616

1717
/// 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].
1820
#[derive(Debug)]
1921
pub struct ConvertError {
2022
kind: ErrorKind,
@@ -23,12 +25,12 @@ pub struct ConvertError {
2325
}
2426

2527
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 {
3032
Self {
31-
kind: ErrorKind::Custom,
33+
kind: ErrorKind::Custom(Some(user_message.into())),
3234
cause: None,
3335
value_str: None,
3436
}
@@ -52,9 +54,10 @@ impl ConvertError {
5254
where
5355
C: Into<Cause>,
5456
{
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+
}
5861
}
5962

6063
/// 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 {
6568
C: Into<Cause>,
6669
V: fmt::Debug,
6770
{
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+
}
7276
}
7377

7478
/// Returns the rust-error that caused this error, if one exists.
@@ -111,12 +115,25 @@ impl Error for ConvertError {
111115
}
112116
}
113117

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+
114131
#[derive(Eq, PartialEq, Debug)]
115132
pub(crate) enum ErrorKind {
116133
FromGodot(FromGodotError),
117134
FromFfi(FromFfiError),
118135
FromVariant(FromVariantError),
119-
Custom,
136+
Custom(Option<String>),
120137
}
121138

122139
impl ErrorKind {
@@ -125,7 +142,7 @@ impl ErrorKind {
125142
Self::FromGodot(from_godot) => Some(from_godot.description()),
126143
Self::FromVariant(from_variant) => Some(from_variant.description()),
127144
Self::FromFfi(from_ffi) => Some(from_ffi.description()),
128-
Self::Custom => None,
145+
Self::Custom(description) => description.clone(),
129146
}
130147
}
131148
}
@@ -135,7 +152,7 @@ impl ErrorKind {
135152
pub(crate) enum FromGodotError {
136153
BadArrayType {
137154
expected: array_inner::TypeInfo,
138-
got: array_inner::TypeInfo,
155+
actual: array_inner::TypeInfo,
139156
},
140157
/// InvalidEnum is also used by bitfields.
141158
InvalidEnum,
@@ -152,32 +169,32 @@ impl FromGodotError {
152169

153170
fn description(&self) -> String {
154171
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!(
159176
"expected array of type {:?}, got array of type {:?}",
160177
expected.variant_type(),
161-
got.variant_type()
162-
);
178+
actual.variant_type()
179+
)
163180
} else {
164-
return format!(
181+
format!(
165182
"expected untyped array, got array of type {:?}",
166-
got.variant_type()
167-
);
168-
}
183+
actual.variant_type()
184+
)
185+
};
169186
}
170187

171188
assert_ne!(
172189
expected.class_name(),
173-
got.class_name(),
190+
actual.class_name(),
174191
"BadArrayType with expected == got, this is a gdext bug"
175192
);
176193

177194
format!(
178195
"expected array of class {}, got array of class {}",
179196
expected.class_name(),
180-
got.class_name()
197+
actual.class_name()
181198
)
182199
}
183200
Self::InvalidEnum => "invalid engine enum value".into(),

0 commit comments

Comments
 (0)