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:
{
"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.