Skip to content

Commit c68af29

Browse files
committed
Implement new undefined/null handling behaviour in drivers
+ code cleaning
1 parent b7801b5 commit c68af29

File tree

2 files changed

+96
-94
lines changed

2 files changed

+96
-94
lines changed

src/driver/express/ExpressDriver.ts

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {AuthorizationCheckerNotDefinedError} from "../../error/AuthorizationChec
1313
import {isPromiseLike} from "../../util/isPromiseLike";
1414
import {getFromContainer} from "../../container";
1515
import {AuthorizationRequiredError} from "../../error/AuthorizationRequiredError";
16+
import { NotFoundError } from "../../index";
1617
const cookie = require("cookie");
1718
const templateUrl = require("template-url");
1819

@@ -228,19 +229,24 @@ export class ExpressDriver extends BaseDriver implements Driver {
228229
}
229230

230231
// set http status code
231-
if (action.undefinedResultCode && result === undefined) {
232-
if (action.undefinedResultCode instanceof Function)
233-
throw new (action.undefinedResultCode as any)(options);
234-
235-
options.response.status(action.undefinedResultCode);
236-
237-
} else if (action.nullResultCode && result === null) {
238-
if (action.nullResultCode instanceof Function)
239-
throw new (action.nullResultCode as any)(options);
240-
241-
options.response.status(action.nullResultCode);
242-
243-
} else if (action.successHttpCode) {
232+
if (result === undefined) {
233+
if (action.undefinedResultCode) {
234+
if (action.undefinedResultCode instanceof Function) {
235+
throw new (action.undefinedResultCode as any)(options);
236+
}
237+
}
238+
}
239+
else if (result === null) {
240+
if (action.nullResultCode) {
241+
if (action.nullResultCode instanceof Function) {
242+
throw new (action.nullResultCode as any)(options);
243+
}
244+
options.response.status(action.nullResultCode);
245+
} else {
246+
options.response.status(204);
247+
}
248+
}
249+
else if (action.successHttpCode) {
244250
options.response.status(action.successHttpCode);
245251
}
246252

@@ -259,8 +265,8 @@ export class ExpressDriver extends BaseDriver implements Driver {
259265
}
260266

261267
options.next();
262-
263-
} else if (action.renderedTemplate) { // if template is set then render it
268+
}
269+
else if (action.renderedTemplate) { // if template is set then render it
264270
const renderOptions = result && result instanceof Object ? result : {};
265271

266272
this.express.render(action.renderedTemplate, renderOptions, (err: any, html: string) => {
@@ -275,29 +281,28 @@ export class ExpressDriver extends BaseDriver implements Driver {
275281
}
276282
options.next();
277283
});
278-
279-
} else if (result !== undefined || action.undefinedResultCode) { // send regular result
280-
if (result === null || (result === undefined && action.undefinedResultCode)) {
281-
if (result === null && !action.nullResultCode) {
282-
options.response.status(204);
283-
}
284-
285-
if (action.isJsonTyped) {
286-
options.response.json();
287-
} else {
288-
options.response.send();
289-
}
290-
options.next();
284+
}
285+
else if (result != null) { // send regular result
286+
if (action.isJsonTyped) {
287+
options.response.json(result);
291288
} else {
292-
if (action.isJsonTyped) {
293-
options.response.json(result);
294-
} else {
295-
options.response.send(result);
296-
}
297-
options.next();
289+
options.response.send(result);
290+
}
291+
options.next();
292+
}
293+
else if (result === undefined) {
294+
const notFoundError = new NotFoundError();
295+
if (action.undefinedResultCode) {
296+
notFoundError.httpCode = action.undefinedResultCode as number;
297+
}
298+
throw notFoundError;
299+
}
300+
else { // send null/undefined response
301+
if (action.isJsonTyped) {
302+
options.response.json(null);
303+
} else {
304+
options.response.send(null);
298305
}
299-
300-
} else {
301306
options.next();
302307
}
303308
}

src/driver/koa/KoaDriver.ts

Lines changed: 55 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {isPromiseLike} from "../../util/isPromiseLike";
1313
import {getFromContainer} from "../../container";
1414
import {RoleChecker} from "../../RoleChecker";
1515
import {AuthorizationRequiredError} from "../../error/AuthorizationRequiredError";
16+
import { NotFoundError, HttpError } from "../../index";
1617
const cookie = require("cookie");
1718
const 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

Comments
 (0)