Handling an error with multiple attributes or parameters with JSON API

Viewed 811

JSON API specifies that multiple errors on an attribute/parameter should be listed separately. What is the best way to handle a single error that involves several parameters at once?

For example, let's say an endpoint accepts either bid or fold parameters, but not both (they are mutually exclusive). What should be the error response if both of parameters are submitted (GET /endpoint?bid=100.00&fold=muck)?

List the error twice, once for each attribute?

{
  "errors": [
    {
      "status": "400",
      "source": { "parameter": "bid" },
      "detail": "Cannot accept both 'bid' and 'fold' parameters."
    },
    {
      "status": "400",
      "source": { "parameter": "fold" },
      "detail": "Cannot accept both 'bid' and 'fold' parameters."
    }
  ]
}

Combine the attributes?

{
  "errors": [
    {
      "status": "400",
      "source": { "parameter": ["bid", "fold"] },
      "detail": "Cannot accept both 'bid' and 'fold' parameters."
    }
  ]
}

Make one higher-level error for the entire request?

{
  "errors": [
    {
      "status": "400",
      "source": { "pointer": "/data" },
      "detail": "Cannot accept both 'bid' and 'fold' parameters."
    }
  ]
}

Some other way?

2 Answers

Plan A suffers from the fact that resurrecting the full context of error is now pushed to the caller. For example, if the caller fixed one error and the other "magically" disappeared - that could be potentially confusing.

Within Plan A, you could add meta option to point out the cross-linkage as some extra help.

Plan B in comparison feels correct as it captures the essence of the error scenario (multi-parameter) plus pins them down concisely. Naturally, you will also need to request an API enhancement, probably requesting for an array of source. For example:

"source" : [ {parameter...}, {parameter...} ]
Related