@@ -211,12 +211,10 @@ export class KoaDriver extends BaseDriver implements Driver {
211
211
}
212
212
213
213
// set http status code
214
- if ( action . undefinedResultCode && result === undefined ) {
215
- if ( action . undefinedResultCode instanceof Function ) {
216
- throw new ( action . undefinedResultCode as any ) ( options ) ;
217
- }
214
+ if ( result === undefined && action . undefinedResultCode && action . undefinedResultCode instanceof Function ) {
215
+ throw new ( action . undefinedResultCode as any ) ( options ) ;
218
216
}
219
- else if ( action . nullResultCode && result === null ) {
217
+ else if ( result === null && action . nullResultCode ) {
220
218
if ( action . nullResultCode instanceof Function ) {
221
219
throw new ( action . nullResultCode as any ) ( options ) ;
222
220
}
@@ -240,16 +238,16 @@ export class KoaDriver extends BaseDriver implements Driver {
240
238
241
239
return options . next ( ) ;
242
240
243
- } else if ( action . renderedTemplate ) { // if template is set then render it // todo : not working in koa
241
+ } else if ( action . renderedTemplate ) { // if template is set then render it // TODO : not working in koa
244
242
const renderOptions = result && result instanceof Object ? result : { } ;
245
243
246
244
this . koa . use ( async function ( ctx : any , next : any ) {
247
245
await ctx . render ( action . renderedTemplate , renderOptions ) ;
248
246
} ) ;
249
247
250
248
return options . next ( ) ;
251
-
252
- } else if ( result != null ) { // send regular result
249
+ }
250
+ else if ( result != null ) { // send regular result
253
251
if ( result instanceof Object ) {
254
252
options . response . body = result ;
255
253
} else {
@@ -258,7 +256,14 @@ export class KoaDriver extends BaseDriver implements Driver {
258
256
259
257
return options . next ( ) ;
260
258
}
261
- else { // send null/undefined response
259
+ else if ( result === undefined ) { // throw NotFoundError on undefined response
260
+ const notFoundError = new NotFoundError ( ) ;
261
+ if ( action . undefinedResultCode ) {
262
+ notFoundError . httpCode = action . undefinedResultCode as number ;
263
+ }
264
+ throw notFoundError ;
265
+ }
266
+ else { // send null response
262
267
if ( action . isJsonTyped ) {
263
268
options . response . body = null ;
264
269
} else {
@@ -267,18 +272,10 @@ export class KoaDriver extends BaseDriver implements Driver {
267
272
268
273
// Setting `null` as a `response.body` means to koa that there is no content to return
269
274
// so we must reset the status codes here.
270
- if ( result === null ) {
271
- if ( action . nullResultCode ) {
272
- options . response . status = action . nullResultCode ;
273
- } else {
274
- options . response . status = 204 ;
275
- }
276
- } else if ( result === undefined ) {
277
- const notFoundError = new NotFoundError ( ) ;
278
- if ( action . undefinedResultCode ) {
279
- notFoundError . httpCode = action . undefinedResultCode as number ;
280
- }
281
- throw notFoundError ;
275
+ if ( action . nullResultCode ) {
276
+ options . response . status = action . nullResultCode ;
277
+ } else {
278
+ options . response . status = 204 ;
282
279
}
283
280
284
281
return options . next ( ) ;
0 commit comments