Skip to content

Fix incorrect handling of rejected promises from Middleware.use() #438

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

Closed
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
21 changes: 4 additions & 17 deletions src/driver/koa/KoaDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,24 +328,11 @@ export class KoaDriver extends BaseDriver {
const middlewareFunctions: Function[] = [];
uses.forEach(use => {
if (use.middleware.prototype && use.middleware.prototype.use) { // if this is function instance of MiddlewareInterface
middlewareFunctions.push((context: any, next: (err?: any) => Promise<any>) => {
middlewareFunctions.push(async (context: any, next: (err?: any) => Promise<any>) => {
try {
const useResult = (getFromContainer(use.middleware) as KoaMiddlewareInterface).use(context, next);
if (isPromiseLike(useResult)) {
useResult.catch((error: any) => {
this.handleError(error, undefined, {
request: context.req,
response: context.res,
context,
next
});
return error;
});
}

return useResult;
return await (getFromContainer(use.middleware) as KoaMiddlewareInterface).use(context, next);
} catch (error) {
this.handleError(error, undefined, {
return await this.handleError(error, undefined, {
request: context.request,
response: context.response,
context,
Expand Down Expand Up @@ -423,4 +410,4 @@ export class KoaDriver extends BaseDriver {

return await next();
}
}
}
30 changes: 25 additions & 5 deletions test/functional/koa-middlewares.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,19 @@ describe("koa middlewares", () => {

class TestCustomMiddlewareWhichThrows implements KoaMiddlewareInterface {

use(request: any, response: any, next?: Function): any {
use(context: any, next?: Function): any {
throw new NotAcceptableError("TestCustomMiddlewareWhichThrows");
}

}

class TestCustomAsyncMiddlewareWhichThrows implements KoaMiddlewareInterface {

async use(context: any, next?: Function): Promise<any> {
throw new NotAcceptableError("TestCustomAsyncMiddlewareWhichThrows");
}

}
@Controller()
class KoaMiddlewareController {

Expand Down Expand Up @@ -127,9 +134,15 @@ describe("koa middlewares", () => {
return "1234";
}

@Get("/customMiddlewareWichThrows")
@Get("/customMiddlewareWhichThrows")
@UseBefore(TestCustomMiddlewareWhichThrows)
customMiddlewareWichThrows() {
customMiddlewareWhichThrows() {
return "1234";
}

@Get("/customAsyncMiddlewareWhichThrows")
@UseBefore(TestCustomAsyncMiddlewareWhichThrows)
TestCustomAsyncMiddlewareWhichThrows() {
return "1234";
}

Expand Down Expand Up @@ -193,10 +206,17 @@ describe("koa middlewares", () => {

it("should handle errors in custom middlewares", () => {
return chakram
.get("http://127.0.0.1:3001/customMiddlewareWichThrows")
.get("http://127.0.0.1:3001/customMiddlewareWhichThrows")
.then((response: any) => {
expect(response).to.have.status(406);
});
});

});
it("should handle errors in custom async middlewares", () => {
return chakram
.get("http://127.0.0.1:3001/customAsyncMiddlewareWhichThrows")
.then((response: any) => {
expect(response).to.have.status(406);
});
});
});