Comments (11)
Find the soluton : put buildPromise inside try/catch
async apiHandler(params) {
const { methodName, controller, response, validatedArgs, successStatus, next } = params;
try {
const promise = this.buildPromise(methodName, controller, validatedArgs);
const data = await Promise.resolve(promise);
let statusCode = successStatus;
let headers;
if (this.isController(controller)) {
headers = controller.getHeaders();
statusCode = controller.getStatus() || statusCode;
}
this.returnHandler({ response, headers, statusCode, data });
}
catch (error) {
return next(error);
}
}
from tsoa.
Sorry, but i haven't find a way to add a proper test and I have no more time to pass on it.
It seems the solution has to be completed: when we use inversify (and probably all other solutions ?) we have to await for apiHandler to execute, so errors could be catched and pass to next(err). Otherwise, errors could pass through tr/catch due to async code.
{{#if ../../iocModule}}async {{/if}}templateService.apiHandler({
from tsoa.
Hello there VincentClair 👋
Thank you for opening your very first issue in this project.
We will try to get back to you as soon as we can.👀
from tsoa.
Code example :
import * as express from 'express';
import * as http from 'http';
import { APIError } from '../Error/APIError';
const app = express();
const server = http.createServer(app);
RegisterRoutes(app);
app.use((
err: APIError,
req: express.Request,
res: express.Response,
next: express.NextFunction,
) => {
try {
if (res.headersSent) {
next(err);
return;
}
res.status(err.code)
.json({ success: false });
} catch (e) {
console.debug(e);
res.status(500);
next(e);
}
});
server.listen(8080, () => {
logger.info(`Server listening on port: ${port}`);
});
import { Get, Route, Security } from 'tsoa';
import { APIError, ForbiddenAPIError, provideSingleton } from '../../src';
@Route('test')
@provideSingleton(TestController)
export class TestController {
@Get('/success')
public success(): Promise<{ success: true }> {
// works as expected
return Promise.resolve({ success: true });
}
@Get('/conflict')
public conflict(): Promise<void> {
// fails / hangs
throw new APIError('test-conflict', null, 409);
}
}
from tsoa.
@VincentClair thank you for locate the missing part, could you submit a PR including test cases let tsoa prevent such issue in the future?
from tsoa.
@jackey8616 have you an advice how to test this case ?
I suppose it's more an integration test to put in *-server.spec.ts ?
from tsoa.
@jackey8616 have you an advice how to test this case ?
I suppose it's more an integration test to put in *-server.spec.ts ?
Yes, you should add one test case in express, koa, hapi integration test.
You can take fixtures/getController.ts as example, duplicate the method with throwing an error and add handle case in .spec.ts of 3 of that framework.
from tsoa.
Sorry, but i haven't find a way to add a proper test and I have no more time to pass on it.
It seems the solution has to be completed: when we use inversify (and probably all other solutions ?) we have to await for apiHandler to execute, so errors could be catched and pass to next(err). Otherwise, errors could pass through tr/catch due to async code.
{{#if ../../iocModule}}async {{/if}}templateService.apiHandler({
Never mind, I will take rest of things, thanks for your informations.
I will submit a PR to fix this ASAP.
from tsoa.
@VincentClair
can you provides more context, everything seems works fine with error handler.
test cases is covered with throwing error inside controller, and it can be correctly catched from outside global error handler.
from tsoa.
Finally found the problem: if the throwing method is not async, error is neither caught by apiHandler
nor generated template.
I have made a PR #1602
from tsoa.
@WoH When did you plan to publish the fix please ?
from tsoa.
Related Issues (20)
- programmatic way to generate the spec and routes HOT 13
- Custom parameters in controller HOT 4
- Evaluate @Security in order of definition HOT 4
- AuthenticationMiddleware should also depend on authenticationModule being present HOT 2
- Unable to assign null to properties defined as number | null | undefined after updating to version 6.2.0 HOT 3
- Reasoning for always setting defaultNumberType HOT 2
- Can't send stream to injected error responder when using '@Security()` decorator. (Really I'm just trying to create a plain-text error) HOT 2
- Declare Auth schema without need to implement handlers HOT 2
- Make express request injectable HOT 3
- Problem with @types/express version 4.17.21 HOT 2
- Upload multiple files using @UploadedFiles() and @UploadedFile causes multer error HOT 2
- Using combination of @UploadedFile() and @UploadedFiles() decorators throws MulterError: Unexpected field HOT 3
- GenerateMetadataError: Unknown type: IndexedAccessType HOT 6
- GenerateMetadataError: Unknown type: IntrinsicKeyword HOT 3
- Routes.ts TSC Error expressAuthenticationRecasted HOT 1
- Multiple runtime dependencies for backend frameworks. HOT 1
- nullable: true and allOf HOT 1
- Support verbatimModuleSyntax (tsconfig) HOT 2
- Regression in route generation from 5.1.1 to 6.x.x: "This node kind is unknown"
- CLI cannot import tsoa-config.js in ESM project HOT 2
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.