File size: 1,203 Bytes
d76f93d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { Request, Response, NextFunction } from "express";
import { AppError } from "../utils/apiErrors";
import { wrapError } from "../utils/responseWrapper";
import { ValidateError } from "tsoa";

export function errorHandler(
  err: Error,
  req: Request,
  res: Response,
  next: NextFunction
) {   
  if (err instanceof ValidateError) {
    console.warn(`Validation error for ${req.path}:`, err.fields);
    
    const messages = Object.entries(err.fields)
      .map(([field, fieldError]) => {
        return `${field}: ${fieldError.message}`;
      })
      .join(", ");

    return res.status(400).json(
      wrapError(
        `Validation failed: ${messages}`,
        400,
        "VALIDATION_ERROR"
      )
    );
  }

  if (err instanceof AppError) {
    return res.status(err.statusCode).json(
      wrapError(err.message, err.statusCode, err.code)
    );
  }

  console.error("Unexpected error:", err);

  return res.status(500).json(
    wrapError("Internal server error", 500, "INTERNAL_ERROR")
  );
}

export function notFoundHandler(
  req: Request,
  res: Response,
  next: NextFunction
) {
  res.status(404).json(
    wrapError(`Route ${req.url} not found`, 404, "NOT_FOUND")
  );
}