Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle errors by throwing them #1631

Closed
2 tasks done
edwinm opened this issue May 23, 2024 · 3 comments
Closed
2 tasks done

Handle errors by throwing them #1631

edwinm opened this issue May 23, 2024 · 3 comments
Labels

Comments

@edwinm
Copy link

edwinm commented May 23, 2024

Sorting

  • I'm submitting a ...

    • feature request
  • I confirm that I

    • used the search to make sure that a similar issue hasn't already been submit

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:

@Throws(404, "Not found");
public async getAppDetails(@Path() slug: string): Promise<AppDetails | undefined> {

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.

Copy link

Hello there edwinm 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

@WoH
Copy link
Collaborator

WoH commented Jun 7, 2024

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

Copy link

github-actions bot commented Jul 8, 2024

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants