Custom HTTP status codes in tsoa or any alternative

556 Views Asked by At

Is it possible to somehow return custom status codes (eg 600) with TSOA? This was possible in the past (v3.5.2), but isn't anymore with the latest versions. I imagine this is unexpected behavior, since TSOA is implementing the OpenAPI specification with the allowed status codes being these ones.

Trying different status codes with decorators like so @Response<IErrorDomain>('600', 'Custom Error') yields following build error: Argument of type '"600"' is not assignable to parameter of type 'HttpStatusCodeLiteral | HttpStatusCodeStringLiteral | OtherValidOpenApiHttpStatusCode'

Is there any way to achieve this conveniently however with the latest versions? If not, is there any alternative to TSOA where swagger docs are generated from express endpoints but with the possibility to use custom status codes?

Thanks in advance.

1

There are 1 best solutions below

0
On

You shouldn't be returning a non-standard error code outside the normal range, that defeats the whole purpose of having universal http error codes. If you really can't find an existing code that apply to your use case, use something in the 4XX or 5XX range (generally 2XX are considered successes, 3XX semi success, but further action needs to be made), which are unused and specify something like

@Response<IErrorDomain>('4XX', 'Custom Error')

In the body of the response, you could then have your own specific codes. Don't use http error codes as application error codes.