eletrotupi / tcc/ commit / 33435d3

api: drop error handler's log from leaking into test envs

Pedro Lucas Porcellis porcellis@eletrotupi.com · 6 months ago 33435d313f82e2cc63720eb0f82c7c016427f2df Browse files
Parents d4d62db
Changed files
2
Additions
+5
Deletions
-2
api/src/middleware/auth.ts
1 1
import { Request, Response, NextFunction } from 'express';
2 2
import { verifyToken } from '@app/lib/jwt';
3 3

4 4
export interface AuthenticatedRequest extends Request {
5 5
  userId?: number;
28 28
    if (err.name === 'TokenExpiredError') {
29 29
      return res.status(401).json({ error: 'Unauthorized: Token expired' });
30 30
    }
31 31
    next(err);
32 32
  }
33 -
};
33 +
};
api/src/middleware/errorHandler.ts
1 1
import { Request, Response, NextFunction } from 'express';
2 2
import { Prisma } from '@prisma/client' // Bleh
3 3
import { isDomainError } from "@app/lib/errors/base";
4 4

5 5
export const errorHandler = (
6 6
  err: unknown,
7 7
  _req: Request,
8 8
  res: Response,
9 9
  _next: NextFunction
10 10
) => {
11 -
  // TODO: Rig some improved, centralized error logging here
12 -
  console.error(`[ErrorHandler]: ${err}`);
11 +
  if (process.env.NODE_ENV === "development") {
12 +
    // TODO: Rig some improved, centralized error logging here
13 +
    console.error(`[ErrorHandler]: ${err}`);
14 +
  }
13 15

14 16
  if (isDomainError(err)) {
15 17
    return res.status(err.status)
16 18
      .json({ error: err.message });
17 19
  }
25 27
      .json({ error: "Unique constraint violation" });
26 28
  }
27 29
  
28 30
  res.status(500).json({ error: "Internal server error" });
29 31
}