feat: improve error handling for API

pull/190/head
tycrek 2 years ago
parent 642e84910b
commit 93d6cfd930
No known key found for this signature in database
GPG Key ID: FF8A54DCE404885A

@ -21,11 +21,20 @@ const RouterApi = Router();
*/ */
const errorHandler = (res: Response, err: Error | any) => { const errorHandler = (res: Response, err: Error | any) => {
log.error(err); log.error(err);
// Get the status code for the Response
let code: number;
switch (err.message) { switch (err.message) {
case 'User not found': return res.sendStatus(404); case 'User not found':
case 'Meta key already exists': return res.sendStatus(409); code = 404; break;
default: return res.sendStatus(500); case 'Meta key already exists':
case 'Username already taken':
code = 409; break;
default:
code = 500;
} }
return res.status(code).type('text').send(err.message ?? err);
}; };
/** /**

Loading…
Cancel
Save