-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update/type errors #360
Update/type errors #360
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ccali11 Type changes look good. Error handling could be improved by using built-in Express functionality: see here. Looks like some spots do require custom errors (a user is not found and the returned rows are empty, we throw a user not found and handle accordingly in the app side with a try catch). Don't think we need to both return custom errors sometimes in json response and sometimes throw. Probably just return valid or undefined data or throw.
Ok so basically remove try catches from express routes and replace with error handling middleware? |
@shanejearley - So put another way (and to use an example), are you suggesting you prefer the first option to the second? router.post('/nonce', async (req: express.Request, res: express.Response, next: express.NextFunction) => {
try {
const { address } = req.body
const nonce = await upsertNonce(address)
if (nonce) {
res.setHeader('Content-Type', 'text/plain')
res.status(200).json({
error: false,
message: 'Nonce retrieved',
data: nonce
})
}
} catch (error: any) {
next(error)
}
}) router.post('/nonce', async (req: express.Request, res: express.Response, next: express.NextFunction) => {
try {
const { address } = req.body;
const nonce = await upsertNonce(address);
if (nonce) {
res.setHeader('Content-Type', 'text/plain');
res.status(200).send(nonce);
} else {
res.status(404).json({
error: true,
message: 'Error getting nonce'
});
}
} catch (error: any) {
next(error);
}
}); |
I like the first example for route handlers, since For that example, do we want to have |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love it!, Will work to incorporate as much as i can to the front-end!
Shane - @DemogorGod and I are meeting on Thursday to do the integration work. If I don't have a good pattern already, I think the necessary and appropriate error handling required will become very apparent then. For now, I tried to follow a few principles, which were essentially to:
ApiResponse
)Looking forward to feedback. Ping me if anything is confusing and we can talk through anything that is unclear or not best practice in your eyes.