Closed
Description
Say I need to convert a Custom Error into FieldError
with extensions, I will do:
#[derive(thiserror::Error, Debug, Deserialize, Serialize)]
#[serde(tag = "type", content = "data")]
enum SomeError {
#[error("SomeError Item: arg1: {arg1}, arg2: {arg2}")]
Item { arg1: String, arg2: i32 },
}
impl IntoFieldError for SomeError {
fn into_field_error(self) -> juniper::FieldError {
juniper::FieldError::new(
"Get Some Error",
juniper::graphql_value!({
"type": "SomeError",
"arg1": &self.arg1,
"arg2": &self.arg2,
}),
)
}
}
However, this will not compile, maybe because the macro does not support varients...
One walkround is:
juniper::FieldError::new(
"Get Some Error",
juniper::Value::object(
vec![
("type","SomeError"),
("arg1", juniper::Value::scalar(&self.arg1)),
("arg2", juniper::Value::scalar(&self.arg2)),
]
.into_iter()
.collect(),
),
)
But of course, it's not as clear as the former one...