haskell / haskell/aeson

Custom error type

Open
#683 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
1.3k
Forks
336
Avg merge
3d 7h
Merged PRs (30d)
4

Description

We're using Haskell to write one of our services, and we're using Aeson for input-json parsing.

However, the API of the platform as a whole _must_ expose standardised validation errors like my example:

```javascript
{
"errors": [
{
"code": "ERR.SOMEFIELD.INVALID",
"field": "foo",
"type": "field",
"message": "Expecting an 'int' but got 'string'"
},
{
"code": "ERR.PLAN.INVALID_NAME",
"type": "field",
"field" : "user.plan",
"message": "All plan names must start with a 'urn:plan'" // nullable
}
],
"service": "SomeServiceIdentifier/version"
}
```
I see that in Aeson this is not possible, since the `Error` data constructor only uses `String` for its output errors. Also, my understanding is that accumulating JSON errors is achieved using `(<*>+)` which is quite cool.

What I'd like to understand is how we achieve some sort of a custom error type that can be represented like the example above, if possible at all. The naive approach would be to use `fail` with some sort of standardized string and then parse the custom error fields out of it, but that is rather unsatisfactory.

Is there a way to achieve this?

#

Edit: I'd like to contribute to this, but I'm quite new to Haskell.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.