express-openapi-validator icon indicating copy to clipboard operation
express-openapi-validator copied to clipboard

Return errors thrown in serDes serializers/deserializer

Open marcocastignoli opened this issue 2 years ago • 1 comments

Describe the bug The system is throwing an incorrect error message. Instead of the custom error message thrown in the serializer, the system is returning a generic OpenAPI validation error. Actually, I don't know if it is an intended feature or a bug.

To Reproduce

  1. Pass a chain argument to the serializer that is not supported for verification.
  2. The system returns an OpenAPI validation error instead of the custom error message.

Actual behavior The system returns the following error message:

{
  "message": "request/body/chain format is invalid",
  "errors": [
    {
      "path": "/body/chain",
      "message": "format is invalid",
      "errorCode": "serdes.openapi.validation"
    }
  ]
}

Expected behavior The system should return the custom error message thrown in the serializer:

Chain ${chain} not supported for verification!

Examples and context Here's a snippet of the code of the serializer


//serDes
          {
            format: "supported-chains",
            deserialize: (chain: string) => checkSupportedChainId(chain),
            serialize: () => (chain: string) => chain,
          },

// checkSupportedChainId
export function checkSupportedChainId(chain: string): string {
  if (!(chain in sourcifyChainsMap && sourcifyChainsMap[chain].supported)) {
    throw new Error(`Chain ${chain} not supported for verification!`);
  }

  return chain;
}

This bug suggests that our error handling is not capturing and returning the appropriate messages from our application logic. Instead, it's defaulting to the more general OpenAPI validation error. We may need to check our error handling flow or how we're mapping application errors to our API responses.

marcocastignoli avatar May 11 '23 07:05 marcocastignoli

I think the problem is here: https://github.com/cdimascio/express-openapi-validator/blob/01950b7558209a6ecefeea5699d5a30c05023662/src/framework/ajv/index.ts#L83-L96

The catched error is completally lost

marcocastignoli avatar May 11 '23 09:05 marcocastignoli