@@ -13,6 +13,7 @@ import {isPromiseLike} from "../../util/isPromiseLike";
1313import { getFromContainer } from "../../container" ;
1414import { RoleChecker } from "../../RoleChecker" ;
1515import { AuthorizationRequiredError } from "../../error/AuthorizationRequiredError" ;
16+ import { NotFoundError , HttpError } from "../../index" ;
1617const cookie = require ( "cookie" ) ;
1718const templateUrl = require ( "template-url" ) ;
1819
@@ -211,17 +212,14 @@ export class KoaDriver extends BaseDriver implements Driver {
211212
212213 // set http status code
213214 if ( action . undefinedResultCode && result === undefined ) {
214- if ( action . undefinedResultCode instanceof Function )
215+ if ( action . undefinedResultCode instanceof Function ) {
215216 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 ) {
221221 throw new ( action . nullResultCode as any ) ( options ) ;
222-
223- options . response . status = action . nullResultCode ;
224-
222+ }
225223 } else if ( action . successHttpCode ) {
226224 options . response . status = action . successHttpCode ;
227225 }
@@ -251,34 +249,38 @@ export class KoaDriver extends BaseDriver implements Driver {
251249
252250 return options . next ( ) ;
253251
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+ }
256258
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+ }
262267
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 ) {
264271 if ( action . nullResultCode ) {
265272 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 ;
275273 } 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 ;
277280 }
278- return options . next ( ) ;
281+ throw notFoundError ;
279282 }
280283
281- } else {
282284 return options . next ( ) ;
283285 }
284286 }
@@ -287,38 +289,33 @@ export class KoaDriver extends BaseDriver implements Driver {
287289 * Handles result of failed executed controller action.
288290 */
289291 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+ }
304300
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+ }
311307
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+ }
318314
319- return Promise . resolve ( ) ;
320- }
321- return Promise . reject ( error ) ;
315+ return resolve ( ) ;
316+ }
317+ return reject ( error ) ;
318+ } ) ;
322319 }
323320
324321 // -------------------------------------------------------------------------
0 commit comments