You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can throw and set an express error handling middleware to catch the error. Note that you have to document the error response format manually on the Endpoint though
Sorting
I'm submitting a ...
I confirm that I
Feature request
Current situation
The documentation is a bit sparse regarding handling errors.
In have a GET method like this:
public async getAppDetails(@Path() slug: string, @Res() notFoundResponse: TsoaResponse<404, { reason: string }>): Promise<AppDetails | undefined> {
and when an object is not found, I do
return notFoundResponse(404, { reason:
No app with slug '${slug}' found});
This is quite verbose.
Improved situation
Why not just throw an error? I know Exceptions are not reflected in TypeScript, so a annotation is still needed, but this looks more clear:
and when an object is not found:
throw new TsoaError({code: 404, reason:
No app with slug '${slug}' found});
When tsoa catches the error, it creates a Response with a 404 code and the reason.
Reasoning
Especially with multiple possible error codes, having them all as an argument doesn't make the code better.
Also, throwing an error makes it very clear what the intention is.
Other solutions
Maybe tsoa already has better options than the example I provided for the current situation.
In that case, please expand the documentation because I couldn't find much about handling errors.
The text was updated successfully, but these errors were encountered: