Skip to content

src/middleware/error-handler.ts โ€‹

From Week 5 in-class: testing ยท original: si-679-f-26-week05-nyt-jessicadonoho/src/middleware/error-handler.ts ยท all files in this project

ts
import type { NextFunction, Request, Response } from 'express';
import { ValidationError } from '../errors.js';

export const errorHandler = (
  err: Error,
  req: Request,
  res: Response,
  next: NextFunction
): void => {
  if (err instanceof ValidationError) {
    res.status(400).json({ error: err.message });
    return;
  }
  res.status(500).json({ error: 'Something went wrong' });
};