TSOA multiple responses for same status code

1.9k Views Asked by At

I want to use the same error response status code twice for some endpoints (I dont want to debate if this is correct or not). Suppose I have two different types of conflicts, I would want to return two separate 409 errors. This is supported as of OpenAPI 3, which I am using. I can see how to manually add an additional error schema in the generated swagger, but I cannot see how to do this in TSOA.

TSOA simply will show the error defined last, and omit any preceding it. If I cannot have multiple errors with the same code in TSOA, is there a way I can add an additional example error? Here I am talking about when you load your spec into an editor, you get an 'examples' drop down - can I add my other 409 error here? (see image). enter image description here

This is how I define my errors.

@Response<ErrorBody>('409', 'A 409 error', {
  type: 'https://someurl.com',
  status: 409,
  code: 'error/409-error-one',
  title: 'This is an example',
  detail: 'You should provide error detail',
})
@Response<ErrorBody>('409', 'A 409 error called fred', {
  type: 'https://someurl.com',
  status: 409,
  code: 'error/409-error-one-hundred-and-fifty',
  title: 'This is an example of another error',
  detail: 'You should provide error detail for all errors',
})

So although the above works as expected, so the errors are thrown correctly, the documentation generated only shows the last error for each status code.

1

There are 1 best solutions below

0
On

TSOA allows you to use status codes 1XX, 2XX, 3XX, 4XX, 5XX - each one once per endpoint. My solution now for when the above happens, is to use one of these, usually the closest to the status I want to return. E.g.

@Response<ErrorBody>('409', 'A 409 error', {
  type: 'https://someurl.com',
  status: 409,
  code: 'error/409-error-one',
  title: 'This is an example',
  detail: 'You should provide error detail',
})
@Response<ErrorBody>('4XX', 'A 409 error called fred', {
  type: 'https://someurl.com',
  status: 409,
  code: 'error/409-error-one-hundred-and-fifty',
  title: 'This is an example of another error',
  detail: 'You should provide error detail for all errors',
})

In my setup above I also copy the status code into the error body, so anybody viewing the spec, should be able to work out whats going on.