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

Await apiHandler to handle error in try/catch #1602

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/cli/src/routeGeneration/templates/express.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function RegisterRoutes(app: Router) {
...(fetchMiddlewares<RequestHandler>({{../name}})),
...(fetchMiddlewares<RequestHandler>({{../name}}.prototype.{{name}})),

{{#if ../../iocModule}}async {{/if}}function {{../name}}_{{name}}(request: ExRequest, response: ExResponse, next: any) {
async function {{../name}}_{{name}}(request: ExRequest, response: ExResponse, next: any) {
const args: Record<string, TsoaRoute.ParameterSchema> = {
{{#each parameters}}
{{@key}}: {{{json this}}},
Expand All @@ -103,7 +103,7 @@ export function RegisterRoutes(app: Router) {
const controller = new {{../name}}();
{{/if}}

templateService.apiHandler({
Copy link
Collaborator

@WoH WoH Mar 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can always await
@jackey8616

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, but function must be always async: we could replace condition {{#if ../../iocModule}}async {{/if}}line 82 by async directly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that was the ultimate goal, while still making ts not complain about it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can always await @jackey8616

Yes, I think that's good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

await templateService.apiHandler({
methodName: '{{name}}',
controller,
response,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ type ExpressReturnHandlerParameters = {
export class ExpressTemplateService extends TemplateService<ExpressApiHandlerParameters, ExpressValidationArgsParameters, ExpressReturnHandlerParameters> {
async apiHandler(params: ExpressApiHandlerParameters) {
const { methodName, controller, response, validatedArgs, successStatus, next } = params;
const promise = this.buildPromise(methodName, controller, validatedArgs);

try {
const data = await Promise.resolve(promise);
const data = await this.buildPromise(methodName, controller, validatedArgs);
let statusCode = successStatus;
let headers;
if (this.isController(controller)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ export class HapiTemplateService extends TemplateService<HapiApiHandlerParameter

async apiHandler(params: HapiApiHandlerParameters) {
const { methodName, controller, h, validatedArgs, successStatus } = params;
const promise = this.buildPromise(methodName, controller, validatedArgs);

try {
const data = await Promise.resolve(promise);
const data = await this.buildPromise(methodName, controller, validatedArgs);
let statusCode = successStatus;
let headers;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ type KoaReturnHandlerParameters = {
export class KoaTemplateService extends TemplateService<KoaApiHandlerParameters, KoaValidationArgsParameters, KoaReturnHandlerParameters> {
async apiHandler(params: KoaApiHandlerParameters) {
const { methodName, controller, context, validatedArgs, successStatus } = params;
const promise = this.buildPromise(methodName, controller, validatedArgs);

try {
const data = await Promise.resolve(promise);
const data = await this.buildPromise(methodName, controller, validatedArgs);
let statusCode = successStatus;
let headers;

Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/inversify-cpg/managedController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ export class ManagedController {
public async getModel(): Promise<TestModel> {
return this.managedService.getModel();
}

@Get('ThrowsError')
public getThrowsError(): void {
throw new Error('error thrown');
}
}
10 changes: 10 additions & 0 deletions tests/integration/inversify-glob.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ describe('Inversify Express Server with ControllerPathGlob', () => {
});
});

it('can handle error', async () => {
return verifyGetRequest(
basePath + '/ManagedTest/ThrowsError?tsoa=abc123456',
(_err, res) => {
expect(res.text).to.equal('error thrown');
},
500,
);
});

function verifyGetRequest(path: string, verifyResponse: (err: any, res: request.Response) => any, expectedStatus?: number) {
return verifyRequest(verifyResponse, request => request.get(path), expectedStatus);
}
Expand Down
Loading