fix weird backend bug
This commit is contained in:
@@ -11,7 +11,10 @@ export class ApiError extends Error {
|
||||
}
|
||||
|
||||
const router = Router();
|
||||
const asyncHandler = fn => (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next);
|
||||
const asyncHandler = fn => (req, res, next) => Promise.resolve(fn(req, res, next)).catch(err => {
|
||||
res.error = err;
|
||||
next();
|
||||
});
|
||||
|
||||
const userSchema = Joi.object({
|
||||
username: Joi.string().min(3).max(32).required()
|
||||
|
||||
@@ -48,7 +48,8 @@ app.use("/api/docs", swaggerUi.serve, swaggerUi.setup(specs));
|
||||
app.use("/api", apiRouter);
|
||||
|
||||
// has to be last
|
||||
app.use((err, _req, res) => {
|
||||
app.use((_req, res, _next) => {
|
||||
const err = res.error;
|
||||
if (err instanceof ApiError) {
|
||||
return res.status(err.status).json({ status: "error", error: err.message });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user