Last active
August 14, 2019 23:43
-
-
Save ColeTownsend/6e5bb406c2e075af83a753b0afa570d5 to your computer and use it in GitHub Desktop.
Handling errors with promises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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