mlms13 / bs-decode

Type-safe JSON decoding for ReasonML and OCaml
https://mlms13.github.io/bs-decode/docs/what-and-why
MIT License
103 stars 18 forks source link

Report errors as JSON #117

Open mlms13 opened 1 year ago

mlms13 commented 1 year ago

One common use case for bs-decode is a webserver that decodes the payload of a request. In this case, it would be desirable to send back a 400 response with a response that includes structured errors that are related to the structure of the request.

Something like:

{
  "first_name": {
    "error": "MissingField",
    "description": "Field \"first_name\" is required but was not present"
  },
  "order_ids": [
    {
      "position": 3,
      "error": "InvalidValue",
      "description": "Expected number but found \"3\""
    }
  ]
}

Not sure what the exact structure should be, but it should be something predictable and easy to describe, so that your webserver can document the errors it will return and clients can theoretically do something useful with that data structure.

We already have examples of converting the structured ParseError type to a string in a way that preserves some of the nesting structure, so I think this should be pretty doable.

mlms13 commented 1 year ago

In looking at that example, I think it's important that we use the "error" field consistently so that it can be a discriminator to reconstruct the original error data. Specifically, looking at "order_ids" in that example, there's not enough information to expect the value to be an array.