lodestar icon indicating copy to clipboard operation
lodestar copied to clipboard

API error responses are not spec compliant

Open nflaig opened this issue 1 year ago • 2 comments

API error responses are not compliant with the Beacon API spec

eg.

Expected

curl http://localhost:9596/eth/v1/beacon/states/current/root | jq
{
  "code": 400,
  "message": "Invalid state ID: current"
}

Actual

{
  "statusCode": 400,
  "error": "Bad Request",
  "message": "Invalid block id 'current'"
}

Expected

curl "http://localhost:9596/eth/v1/beacon/pool/attestations?slot=current&committee_index=123" | jq
{
  "code": 400,
  "message": "Invalid slot: current"
}

Actual

[
  {
    "instancePath": "/slot",
    "schemaPath": "#/properties/slot/type",
    "keyword": "type",
    "params": {
      "type": "integer"
    },
    "message": "must be integer"
  }
]

Expected

curl http://localhost:9596/eth/v1/beacon/headers/123 | jq
{
  "code": 404,
  "message": "Block not found"
}

Actual

{
  "statusCode": 404,
  "error": "Not Found",
  "message": "No block found for id '123'"
}

From https://github.com/ChainSafe/lodestar/issues/5710#issuecomment-1860240826

nflaig avatar Feb 25 '24 19:02 nflaig

From my preliminary investigations:

  1. statusCode and error are added by fastify. We should be able to modify it in the handler. Map statusCode to code and ignore error entirely.
  2. data validation errors are formatted and returned by the if (err.validation) {} block in server.setErrorHandler. Just like above we can easily change the format to what we prefer.

I'll take this up after #6293 if the issue is still unassigned.

har777 avatar Feb 26 '24 05:02 har777

For ajv schema errors, we can just override schemaErrorFormatter

nflaig avatar Mar 14 '24 12:03 nflaig