Alex Mikhalev
7 years ago
12 changed files with 47 additions and 27 deletions
@ -1,6 +1,6 @@
@@ -1,6 +1,6 @@
|
||||
import { ErrorCode, toHttpStatus } from "@common/sprinklersRpc/ErrorCode"; |
||||
import { ErrorCode, toHttpStatus } from "@common/ErrorCode"; |
||||
|
||||
export class ApiError extends Error { |
||||
export default class ApiError extends Error { |
||||
name = "ApiError"; |
||||
statusCode: number; |
||||
code: ErrorCode; |
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
import * as express from "express"; |
||||
|
||||
import ApiError from "@common/ApiError"; |
||||
|
||||
const errorHandler: express.ErrorRequestHandler = |
||||
(err: any, req: express.Request, res: express.Response, next: express.NextFunction) => { |
||||
if (err instanceof ApiError) { |
||||
res.status(err.statusCode).json(err.toJSON()); |
||||
} else { |
||||
next(err); |
||||
} |
||||
}; |
||||
|
||||
export default errorHandler; |
Loading…
Reference in new issue