Skip to content

Commit 09a6d6a

Browse files
committed
Merge branch 'koa-middleware-error-handling-fix'
2 parents e7be722 + ab34d5c commit 09a6d6a

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

src/driver/koa/KoaDriver.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,7 @@ export class KoaDriver extends BaseDriver {
255255
options.response.body = Buffer.from(result as any);
256256
}
257257
else { // send regular result
258-
if (result instanceof Object) {
259-
options.response.body = result;
260-
} else {
261-
options.response.body = result;
262-
}
258+
options.response.body = result;
263259
}
264260

265261
// set http status code
@@ -329,24 +325,11 @@ export class KoaDriver extends BaseDriver {
329325
const middlewareFunctions: Function[] = [];
330326
uses.forEach(use => {
331327
if (use.middleware.prototype && use.middleware.prototype.use) { // if this is function instance of MiddlewareInterface
332-
middlewareFunctions.push((context: any, next: (err?: any) => Promise<any>) => {
328+
middlewareFunctions.push(async (context: any, next: (err?: any) => Promise<any>) => {
333329
try {
334-
const useResult = (getFromContainer(use.middleware, { context } as Action) as KoaMiddlewareInterface).use(context, next);
335-
if (isPromiseLike(useResult)) {
336-
useResult.catch((error: any) => {
337-
this.handleError(error, undefined, {
338-
request: context.req,
339-
response: context.res,
340-
context,
341-
next
342-
});
343-
return error;
344-
});
345-
}
346-
347-
return useResult;
330+
return await (getFromContainer(use.middleware) as KoaMiddlewareInterface).use(context, next);
348331
} catch (error) {
349-
this.handleError(error, undefined, {
332+
return await this.handleError(error, undefined, {
350333
request: context.request,
351334
response: context.response,
352335
context,
@@ -424,4 +407,4 @@ export class KoaDriver extends BaseDriver {
424407

425408
return await next();
426409
}
427-
}
410+
}

test/functional/koa-middlewares.spec.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,19 @@ describe("koa middlewares", () => {
6868

6969
class TestCustomMiddlewareWhichThrows implements KoaMiddlewareInterface {
7070

71-
use(request: any, response: any, next?: Function): any {
71+
use(context: any, next?: Function): any {
7272
throw new NotAcceptableError("TestCustomMiddlewareWhichThrows");
7373
}
7474

7575
}
7676

77+
class TestCustomAsyncMiddlewareWhichThrows implements KoaMiddlewareInterface {
78+
79+
async use(context: any, next?: Function): Promise<any> {
80+
throw new NotAcceptableError("TestCustomAsyncMiddlewareWhichThrows");
81+
}
82+
83+
}
7784
@Controller()
7885
class KoaMiddlewareController {
7986

@@ -127,9 +134,15 @@ describe("koa middlewares", () => {
127134
return "1234";
128135
}
129136

130-
@Get("/customMiddlewareWichThrows")
137+
@Get("/customMiddlewareWhichThrows")
131138
@UseBefore(TestCustomMiddlewareWhichThrows)
132-
customMiddlewareWichThrows() {
139+
customMiddlewareWhichThrows() {
140+
return "1234";
141+
}
142+
143+
@Get("/customAsyncMiddlewareWhichThrows")
144+
@UseBefore(TestCustomAsyncMiddlewareWhichThrows)
145+
TestCustomAsyncMiddlewareWhichThrows() {
133146
return "1234";
134147
}
135148

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

194207
it("should handle errors in custom middlewares", () => {
195208
return chakram
196-
.get("http://127.0.0.1:3001/customMiddlewareWichThrows")
209+
.get("http://127.0.0.1:3001/customMiddlewareWhichThrows")
197210
.then((response: any) => {
198211
expect(response).to.have.status(406);
199212
});
200213
});
201214

202-
});
215+
it("should handle errors in custom async middlewares", () => {
216+
return chakram
217+
.get("http://127.0.0.1:3001/customAsyncMiddlewareWhichThrows")
218+
.then((response: any) => {
219+
expect(response).to.have.status(406);
220+
});
221+
});
222+
});

0 commit comments

Comments
 (0)