[QOTD-18] Implement global error-handling middleware #5
11
src/middleware/error.ts
Normal file
11
src/middleware/error.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import type { NextFunction, Request, Response } from 'express';
|
||||||
|
|
||||||
|
export function errorHandler(
|
||||||
|
err: unknown,
|
||||||
|
_req: Request,
|
||||||
|
res: Response,
|
||||||
|
_next: NextFunction
|
||||||
|
): void {
|
||||||
|
console.error(err);
|
||||||
|
res.status(500).json({ error: 'Internal server error' });
|
||||||
|
}
|
||||||
10
src/server.ts
Normal file
10
src/server.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import express from 'express';
|
||||||
|
import { errorHandler } from './middleware/error';
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
|
app.use(errorHandler);
|
||||||
|
|
||||||
|
export default app;
|
||||||
Loading…
Reference in New Issue
Block a user