@@ -13,6 +13,7 @@ import {isPromiseLike} from "../../util/isPromiseLike";
13
13
import { getFromContainer } from "../../container" ;
14
14
import { RoleChecker } from "../../RoleChecker" ;
15
15
import { AuthorizationRequiredError } from "../../error/AuthorizationRequiredError" ;
16
+ import { NotFoundError , HttpError } from "../../index" ;
16
17
const cookie = require ( "cookie" ) ;
17
18
const templateUrl = require ( "template-url" ) ;
18
19
@@ -211,17 +212,14 @@ export class KoaDriver extends BaseDriver implements Driver {
211
212
212
213
// set http status code
213
214
if ( action . undefinedResultCode && result === undefined ) {
214
- if ( action . undefinedResultCode instanceof Function )
215
+ if ( action . undefinedResultCode instanceof Function ) {
215
216
throw new ( action . undefinedResultCode as any ) ( options ) ;
216
-
217
- options . response . status = action . undefinedResultCode ;
218
-
219
- } else if ( action . nullResultCode && result === null ) {
220
- if ( action . nullResultCode instanceof Function )
217
+ }
218
+ }
219
+ else if ( action . nullResultCode && result === null ) {
220
+ if ( action . nullResultCode instanceof Function ) {
221
221
throw new ( action . nullResultCode as any ) ( options ) ;
222
-
223
- options . response . status = action . nullResultCode ;
224
-
222
+ }
225
223
} else if ( action . successHttpCode ) {
226
224
options . response . status = action . successHttpCode ;
227
225
}
@@ -251,34 +249,38 @@ export class KoaDriver extends BaseDriver implements Driver {
251
249
252
250
return options . next ( ) ;
253
251
254
- } else if ( result !== undefined || action . undefinedResultCode ) { // send regular result
255
- if ( result === null || ( result === undefined && action . undefinedResultCode ) ) {
252
+ } else if ( result != null ) { // send regular result
253
+ if ( result instanceof Object ) {
254
+ options . response . body = result ;
255
+ } else {
256
+ options . response . body = result ;
257
+ }
256
258
257
- if ( action . isJsonTyped ) {
258
- options . response . body = null ;
259
- } else {
260
- options . response . body = null ;
261
- }
259
+ return options . next ( ) ;
260
+ }
261
+ else { // send null/undefined response
262
+ if ( action . isJsonTyped ) {
263
+ options . response . body = null ;
264
+ } else {
265
+ options . response . body = null ;
266
+ }
262
267
263
- // todo: duplication. we make it here because after we set null to body koa seems overrides status
268
+ // Setting `null` as a `response.body` means to koa that there is no content to return
269
+ // so we must reset the status codes here.
270
+ if ( result === null ) {
264
271
if ( action . nullResultCode ) {
265
272
options . response . status = action . nullResultCode ;
266
-
267
- } else if ( result === undefined && action . undefinedResultCode ) {
268
- options . response . status = action . undefinedResultCode ;
269
- }
270
-
271
- return options . next ( ) ;
272
- } else {
273
- if ( result instanceof Object ) {
274
- options . response . body = result ;
275
273
} else {
276
- options . response . body = result ;
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 ;
277
280
}
278
- return options . next ( ) ;
281
+ throw notFoundError ;
279
282
}
280
283
281
- } else {
282
284
return options . next ( ) ;
283
285
}
284
286
}
@@ -287,38 +289,33 @@ export class KoaDriver extends BaseDriver implements Driver {
287
289
* Handles result of failed executed controller action.
288
290
*/
289
291
handleError ( error : any , action : ActionMetadata | undefined , options : Action ) : any {
290
- if ( this . isDefaultErrorHandlingEnabled ) {
291
- const response : any = options . response ;
292
- console . log ( "ERROR: " , error ) ;
293
-
294
- // set http status
295
- // note that we can't use error instanceof HttpError properly anymore because of new typescript emit process
296
- if ( error . httpCode ) {
297
- console . log ( "setting status code: " , error . httpCode ) ;
298
- options . context . status = error . httpCode ;
299
- response . status = error . httpCode ;
300
- } else {
301
- options . context . status = 500 ;
302
- response . status = 500 ;
303
- }
292
+ return new Promise ( ( resolve , reject ) => {
293
+ if ( this . isDefaultErrorHandlingEnabled ) {
294
+ // set http status
295
+ if ( error instanceof HttpError && error . httpCode ) {
296
+ options . response . status = error . httpCode ;
297
+ } else {
298
+ options . response . status = 500 ;
299
+ }
304
300
305
- // apply http headers
306
- if ( action ) {
307
- Object . keys ( action . headers ) . forEach ( name => {
308
- response . set ( name , action . headers [ name ] ) ;
309
- } ) ;
310
- }
301
+ // apply http headers
302
+ if ( action ) {
303
+ Object . keys ( action . headers ) . forEach ( name => {
304
+ options . response . set ( name , action . headers [ name ] ) ;
305
+ } ) ;
306
+ }
311
307
312
- // send error content
313
- if ( action && action . isJsonTyped ) {
314
- response . body = this . processJsonError ( error ) ;
315
- } else {
316
- response . body = this . processJsonError ( error ) ;
317
- }
308
+ // send error content
309
+ if ( action && action . isJsonTyped ) {
310
+ options . response . body = this . processJsonError ( error ) ;
311
+ } else {
312
+ options . response . body = this . processTextError ( error ) ;
313
+ }
318
314
319
- return Promise . resolve ( ) ;
320
- }
321
- return Promise . reject ( error ) ;
315
+ return resolve ( ) ;
316
+ }
317
+ return reject ( error ) ;
318
+ } ) ;
322
319
}
323
320
324
321
// -------------------------------------------------------------------------
0 commit comments