Skip to content

Instantly share code, notes, and snippets.

@ColeTownsend
Last active August 14, 2019 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ColeTownsend/6e5bb406c2e075af83a753b0afa570d5 to your computer and use it in GitHub Desktop.
Save ColeTownsend/6e5bb406c2e075af83a753b0afa570d5 to your computer and use it in GitHub Desktop.
Handling errors with promises
// how do I get an error handler function involved here?
Routes.forEach(route => {
(app as any)[route.method](route.route, (req: Request, res: Response, next: Function) => {
const result = (new (route.controller as any))[route.action](req, res, next);
if (result instanceof Promise) {
result.then(result => result !== null && result !== undefined ? res.send(result) : undefined);
} else if (result !== null && result !== undefined) {
res.json(result);
}
});
});
import {UserController} from "./controller/UserController";
export const Routes = [{
method: "get",
route: "/users",
controller: UserController,
action: "all"
}, {
method: "get",
route: "/users/:id",
controller: UserController,
action: "one"
}, {
method: "post",
route: "/users",
controller: UserController,
action: "save"
}, {
method: "delete",
route: "/users/:id",
controller: UserController,
action: "remove"
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment