Skip to content

Commit 57c3c6f

Browse files
committed
cleaner error handling
1 parent 92f5bbc commit 57c3c6f

File tree

1 file changed

+6
-20
lines changed
  • src/serializers/type_serializers

1 file changed

+6
-20
lines changed

src/serializers/type_serializers/union.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl TypeSerializer for UnionSerializer {
154154
exclude: Option<&Bound<'_, PyAny>>,
155155
extra: &Extra,
156156
) -> Result<S::Ok, S::Error> {
157-
match union_serialize(
157+
union_serialize(
158158
|comb_serializer, new_extra| comb_serializer.to_python(value, include, exclude, new_extra),
159159
|v| {
160160
infer_serialize(
@@ -168,15 +168,8 @@ impl TypeSerializer for UnionSerializer {
168168
extra,
169169
&self.choices,
170170
self.retry_with_lax_check(),
171-
) {
172-
Ok(v) => v,
173-
// TODO: we don't expect to hit this branch, but if we do, we should change the return
174-
// type of this function to return a PyResult...
175-
Err(err) => {
176-
let message = err.to_string();
177-
Err(serde::ser::Error::custom(message))
178-
}
179-
}
171+
)
172+
.map_err(|err| serde::ser::Error::custom(err.to_string()))?
180173
}
181174

182175
fn get_name(&self) -> &str {
@@ -338,7 +331,7 @@ impl TypeSerializer for TaggedUnionSerializer {
338331
}
339332
}
340333

341-
match union_serialize(
334+
union_serialize(
342335
|comb_serializer, new_extra| comb_serializer.to_python(value, include, exclude, new_extra),
343336
|v| {
344337
infer_serialize(
@@ -352,15 +345,8 @@ impl TypeSerializer for TaggedUnionSerializer {
352345
extra,
353346
&self.choices,
354347
self.retry_with_lax_check(),
355-
) {
356-
Ok(v) => v,
357-
// TODO: we don't expect to hit this branch, but if we do, we should change the return
358-
// type of this function to return a PyResult...
359-
Err(err) => {
360-
let message = err.to_string();
361-
Err(serde::ser::Error::custom(message))
362-
}
363-
}
348+
)
349+
.map_err(|err| serde::ser::Error::custom(err.to_string()))?
364350
}
365351

366352
fn get_name(&self) -> &str {

0 commit comments

Comments
 (0)