sprinklers3/server/express/errorHandler.ts
Alex Mikhalev 8756180ad1
All checks were successful
continuous-integration/drone the build was successful
fix: icons generation, 404 errors, static serving
2018-12-11 17:03:44 -08:00

38 lines
921 B
TypeScript

import * as express from "express";
import ApiError from "@common/ApiError";
import { ErrorCode } from "@common/ErrorCode";
const isDev = process.env.NODE_ENV === "development";
const errorHandler: express.ErrorRequestHandler = (
err: any,
req: express.Request,
res: express.Response,
next: express.NextFunction
) => {
if (err instanceof ApiError) {
// TODO: different content-type?
res.status(err.statusCode).json(err.toJSON(isDev));
} else if (err) {
let error: ApiError;
if (err.code === "ENOENT") {
error = new ApiError(
"The specified resource could not be found",
ErrorCode.NotFound,
err
);
} else {
error = new ApiError(
"An internal server error has occurred",
ErrorCode.Internal,
err.stack ? err.stack : err
);
}
errorHandler(error, req, res, next);
}
next();
};
export default errorHandler;