Coder Social home page Coder Social logo

Comments (8)

tbranyen avatar tbranyen commented on April 28, 2024 1

I ended up registering an express error middleware and was able to render JSON instead of a string:

// Render error instead of a string `[object Object]`.
app.use((err, req, res, next) => {
  res.status(err.status);
  res.json(err);
});

Hope this helps someone else.

from tsoa.

lukeautry avatar lukeautry commented on April 28, 2024

@pabloelustondo It should be possible to use custom errors. Check out https://github.com/lukeautry/tsoa/blob/master/tests/fixtures/server.ts#L26 as an example; this is just a standard Express convention.

The generated library code will call next if an error is thrown either in the built-in validation steps or inside your code itself. I've added some tests and examples

Let me know if you're still having trouble with this.

from tsoa.

pabloelustondo avatar pabloelustondo commented on April 28, 2024

Thanks Luke. But yes, I am still struggling to understand how to pass the control back in my controller after an invalid parameter has been detected by the router. Not sure I explained my propblem well. Let say my GET controller looks like this:

@get('routename')
public async Get(dateFrom: string, dateTo: string): Promise {

When the router finds and error, I know it will call the 'next' but my problem is that I do not understand how to define this 'next' from my controller.

Sorry for my newbiness

from tsoa.

lukeautry avatar lukeautry commented on April 28, 2024

No problem. If it's confusing to you it's probably confusing to others too.

Yeah, so the controller itself (and any code it calls into) doesn't and shouldn't know anything about the "next" error handling. From the controller, you should just be throwing errors.

When the router finds and error, I know it will call the 'next' but my problem is that I do not understand how to define this 'next' from my controller.

Here are the scenarios I can think of - let me know which one of them sounds like yours.

  • Consumer calls API, but the route doesn't exist. Express itself throws an error; there's no way from your controllers to define what, exactly, the nature of the error message is here. I believe it is possible to customize "route not found" behavior for Express, but that's not within the scope of this project.
  • Consumer calls API, a route does exist, but the parameters passed fail validation. In this case, it's also not currently possible to override or define the nature of the error message; tsoa throws an "InvalidRequestException", which will contain a message about how validation failed, and is a '400' status code error.
  • Consumer calls API, a route does exist, validation of parameters succeeds, but an error is thrown within a controller method. In this case, the error you throw does get passed all the way back up to the router and rendered as a response.

If you are case #3, then I think you're all set. If you're case #2, you will either have to settle for the built in tsoa errors, or I can look into allowing custom error types for validation errors.

from tsoa.

lukeautry avatar lukeautry commented on April 28, 2024

@pabloelustondo Closing for now, let me know if the last explanation was off base.

from tsoa.

nemes1s avatar nemes1s commented on April 28, 2024

@lukeautry Thanks for the explanation, but i have a #2 case scenario (validation fails), but the data returned to API consumer is: [object Object] with status code 400.

Though while debugging step by step, i see the validation error in routes.ts within try catch block and fails as expected. But for some reason it doesn't render the correct response for the consumer with failed messages.

Please help :)

from tsoa.

nemes1s avatar nemes1s commented on April 28, 2024

Ok, don't bother, found my mistake

from tsoa.

tbranyen avatar tbranyen commented on April 28, 2024

@nemes1s it'd be more helpful if you had posted what you found instead of saying you found your mistake. What mistake? What did you do to fix this error?

from tsoa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.