Skip to content

Commit

Permalink
Add basic error handling to api
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewIOM committed Nov 19, 2023
1 parent 2df5659 commit fbb098b
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { ExpressAdapter } from '@bull-board/express';
import { BullMQAdapter } from '@bull-board/api/bullMQAdapter';
import bodyParser = require('body-parser');
import config from 'config';
import express from 'express';
import express, {
Response as ExResponse,
Request as ExRequest,
NextFunction,
} from "express";
import { ValidateError } from "tsoa";
import swaggerUi from 'swagger-ui-express';
import { logger } from './logger';
import { RegisterRoutes } from './routes/routes';
Expand All @@ -28,6 +33,28 @@ app.use(bodyParser.json());

RegisterRoutes(app);

app.use(function errorHandler(
err: unknown,
req: ExRequest,
res: ExResponse,
next: NextFunction
): ExResponse | void {
if (err instanceof ValidateError) {
console.warn(`Caught Validation Error for ${req.path}:`, err.fields);
return res.status(422).json({
message: "Validation Failed",
details: err?.fields,
});
}
if (err instanceof Error) {
return res.status(500).json({
message: "Internal Server Error",
});
}

next();
});

app.use('/admin/queues', serverAdapter.getRouter());
app.use('/', swaggerUi.serve as any, swaggerUi.setup(x) as any);

Expand Down

0 comments on commit fbb098b

Please sign in to comment.