2020-04-15 03:15:30 +00:00
|
|
|
# Error handling
|
2020-05-09 17:53:41 +00:00
|
|
|
|
2020-09-01 05:47:22 +00:00
|
|
|
Resolve can return a `FieldResult`, which has the following definition:
|
2020-05-09 17:53:41 +00:00
|
|
|
|
|
|
|
```rust
|
|
|
|
type FieldResult<T> = std::result::Result<T, FieldError>;
|
|
|
|
```
|
|
|
|
|
2020-09-01 05:47:22 +00:00
|
|
|
Any `Error` that implements `std::fmt::Display` can be converted to `FieldError` and you can extend the error message.
|
2020-05-09 17:53:41 +00:00
|
|
|
|
2020-09-01 05:47:22 +00:00
|
|
|
The following example shows how to parse an input string to an integer. When parsing fails, it will return an error and attach an error message.
|
|
|
|
See the [Error Extensions](error_extensions.md) section of this book for more details.
|
2020-05-09 17:53:41 +00:00
|
|
|
|
|
|
|
```rust
|
|
|
|
use async_graphql::*;
|
|
|
|
|
|
|
|
struct Query;
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-05-09 17:53:41 +00:00
|
|
|
impl Query {
|
|
|
|
#[field]
|
|
|
|
async fn parse_with_extensions(&self, input: String) -> FieldResult<i32> {
|
|
|
|
Ok("234a"
|
|
|
|
.parse()
|
2020-05-12 09:05:25 +00:00
|
|
|
.map_err(|err: ParseIntError| err.extend_with(|_| json!({"code": 400})))?)
|
2020-05-09 17:53:41 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-11 12:22:20 +00:00
|
|
|
```
|