fix: change dev message to code

This commit is contained in:
puriphat 2024-06-14 04:39:48 +00:00
parent ddec8275d8
commit 9722fab149
2 changed files with 6 additions and 15 deletions

View file

@ -1,29 +1,20 @@
import HttpStatus from "./http-status"; import HttpStatus from "./http-status";
type DevMessage =
| "missing_or_invalid_parameter"
| "data_exists"
| "data_in_used"
| "no_permission"
| "unknown_url"
| "data_not_found"
| "unauthorized";
class HttpError extends Error { class HttpError extends Error {
/** /**
* HTTP Status Code * HTTP Status Code
*/ */
status: HttpStatus; status: HttpStatus;
message: string; message: string;
devMessage?: DevMessage; code?: string;
constructor(status: HttpStatus, message: string, devMessage?: DevMessage) { constructor(status: HttpStatus, message: string, code?: string) {
super(message); super(message);
this.name = "HttpError"; this.name = "HttpError";
this.status = status; this.status = status;
this.message = message; this.message = message;
this.devMessage = devMessage; this.code = code;
} }
} }

View file

@ -8,7 +8,7 @@ function error(error: Error, _req: Request, res: Response, _next: NextFunction)
return res.status(error.status).json({ return res.status(error.status).json({
status: error.status, status: error.status,
message: error.message, message: error.message,
devMessage: error.devMessage, code: error.code,
}); });
} }
@ -17,7 +17,7 @@ function error(error: Error, _req: Request, res: Response, _next: NextFunction)
status: HttpStatus.UNPROCESSABLE_ENTITY, status: HttpStatus.UNPROCESSABLE_ENTITY,
message: "Validation error(s).", message: "Validation error(s).",
detail: error.fields, detail: error.fields,
devMessage: "missing_or_invalid_parameter", code: "validateError",
}); });
} }
@ -26,7 +26,7 @@ function error(error: Error, _req: Request, res: Response, _next: NextFunction)
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
status: HttpStatus.INTERNAL_SERVER_ERROR, status: HttpStatus.INTERNAL_SERVER_ERROR,
message: error.message, message: error.message,
devMessage: "system_error", code: "system_error",
}); });
} }