All files / src/handlers error.ts

90% Statements 18/20
80% Branches 4/5
100% Functions 1/1
90% Lines 18/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 271x       1x 8x       8x 2x 2x 2x 2x   8x 3x 3x 3x 3x   3x 3x 3x 3x 3x  
import { Logger } from "@/Logger";
import { NotFoundError, RequestParamError } from "@/Error";
import { ErrorObject } from "@/types/api";
 
export function handleError(logger: Logger, err, res) {
  if (!err) {
    return;
  }
 
  if (err instanceof RequestParamError) {
    const errObj: ErrorObject = { message: err.message };
    res.status(400).json(errObj);
    return;
  }
 
  if (err instanceof NotFoundError) {
    const errObj: ErrorObject = { message: err.message };
    res.status(404).json(errObj);
    return;
  }
 
  const msg = "internal server error";
  logger.error(msg, { message: err });
  const errObj: ErrorObject = { message: msg };
  res.status(500).json(errObj);
}