Skip to content

Customizing HttpApiDecodeError responses #5465

@Avaq

Description

@Avaq

What is the problem this feature would solve?

I have an existing API that I want to create API specs for using the HttpApi module, before gradually implementing it using the HttpApiBuilder, phasing out its old implementation.

The API specs can be fully customised in what each endpoint responds with by using addSuccess and addError, except for one thing: The HttpApiDecodeError.

This means I'm unable to create an API spec to fully cover my existing API.

I posted this on Discord a while ago, but never got a response.

What is the feature you are proposing to solve the problem?

A setDecodeError method that takes a Schema<HttpApiDecodeError, T> so I can customize how those are encoded:

const HttpApiDecodeErrorFromMyWeirdValidationError =
  Schema.transform(
    MyWeirdValidationError,
    HttpApi.HttpApiDecodeErrorSchema, // <--- exposure of default schema
    { encode: (yours) => ({...}), decode: (mine) => ({...}) }
  )
  
export default HttpApi.make('myApi')
.add(MyApiGroup)
.setDecodeError(HttpApiDecodeErrorFromMyWeirdValidationError) // <--- override
.addError(MyWeirdInternalServerError, { status: 500 })

What alternatives have you considered?

Hacking the response with a middleware, and the OpenAPI specs: #5367 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions