Comments (8)
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.
@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.
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.
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.
@pabloelustondo Closing for now, let me know if the last explanation was off base.
from tsoa.
@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.
Ok, don't bother, found my mistake
from tsoa.
@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)
- Error: Unknown type: BigIntKeyword HOT 5
- Missing Support For Unicode Character Classes HOT 1
- Help with tsoa implementation clean architecture express and typescript HOT 7
- Problem Importing JSON with `@example` in TSOA HOT 2
- How to use Prisma model with tsoa HOT 2
- MetadataGenerator.checkForMethodSignatureDuplicates is not working properly HOT 2
- Add ability to inject our custom multer instance to tsoa HOT 3
- How to generate a swagger file for specific controllers or tags HOT 2
- Endpoint to support multiple api versions HOT 3
- Extended interface doesn't produce an `allOf` type HOT 6
- Decorators not working. swagger json contains empty fields HOT 2
- Mock security decorator for testing purpose with Jest HOT 2
- Upload multiple files with different field names HOT 5
- Complicated error response when validating payload that is of a type that is a union of many types HOT 2
- Add posibility to get a property from the Request - @Request("username") Decorator HOT 1
- Errors are logging PII HOT 3
- @Queries Can't support 'refObject' type HOT 4
- ValidateError does not produce valid swagger.json HOT 3
- Problem with swagger generation, when using generic value in nested generic
- OpenTelemetry for distributed tracing with TSOA HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tsoa.