Extracted routes from controllers, wrote readme's
This commit is contained in:
21
api/src/middleware/errors.ts
Normal file
21
api/src/middleware/errors.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { ErrorRequestHandler } from 'express';
|
||||
import { pick } from 'lodash';
|
||||
|
||||
import { CustomError } from 'errors';
|
||||
|
||||
export const handleError: ErrorRequestHandler = (error, _req, res, _next) => {
|
||||
console.error(error);
|
||||
|
||||
const isErrorSafeForClient = error instanceof CustomError;
|
||||
|
||||
const clientError = isErrorSafeForClient
|
||||
? pick(error, ['message', 'code', 'status', 'data'])
|
||||
: {
|
||||
message: 'Something went wrong, please contact our support.',
|
||||
code: 'INTERNAL_ERROR',
|
||||
status: 500,
|
||||
data: {},
|
||||
};
|
||||
|
||||
res.status(clientError.status).send({ error: clientError });
|
||||
};
|
||||
Reference in New Issue
Block a user