Added error class
This commit is contained in:
parent
6df91f3adc
commit
af2c452e64
44
apps/backend/src/errors/DashboardError.ts
Normal file
44
apps/backend/src/errors/DashboardError.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import { StatusCode } from "hono/utils/http-status";
|
||||
import DashboardErrorParameter from "../types/DashboardErrorParameter";
|
||||
|
||||
class DashboardError extends Error {
|
||||
public readonly errorCode: string;
|
||||
public readonly statusCode: StatusCode = 500;
|
||||
public readonly message: string;
|
||||
public readonly formErrors?: Record<string, string>;
|
||||
public readonly severity: "CRITICAL" | "HIGH" | "MEDIUM" | "LOW" =
|
||||
"CRITICAL";
|
||||
|
||||
constructor(options: DashboardErrorParameter) {
|
||||
super();
|
||||
|
||||
this.errorCode = options.errorCode;
|
||||
this.statusCode = options.statusCode ?? this.statusCode;
|
||||
this.message = options.message;
|
||||
this.formErrors = options.formErrors;
|
||||
|
||||
Object.setPrototypeOf(this, new.target.prototype);
|
||||
}
|
||||
}
|
||||
|
||||
export const notFound = (options?: Partial<DashboardErrorParameter>) => {
|
||||
throw new DashboardError({
|
||||
errorCode: options?.errorCode ?? "NOT_FOUND",
|
||||
message: options?.message ?? "The requested data is not found",
|
||||
formErrors: options?.formErrors,
|
||||
severity: options?.severity ?? "LOW",
|
||||
statusCode: options?.statusCode ?? 404,
|
||||
});
|
||||
};
|
||||
|
||||
export const unauthorized = (options?: Partial<DashboardErrorParameter>) => {
|
||||
throw new DashboardError({
|
||||
errorCode: options?.errorCode ?? "UNAUTHORIZED",
|
||||
message: options?.message ?? "Unauthorized",
|
||||
formErrors: options?.formErrors,
|
||||
severity: options?.severity ?? "LOW",
|
||||
statusCode: options?.statusCode ?? 401,
|
||||
});
|
||||
};
|
||||
|
||||
export default DashboardError;
|
||||
|
|
@ -11,6 +11,7 @@ import { getSignedCookie } from "hono/cookie";
|
|||
import dashboardRoutes from "./routes/dashboard/routes";
|
||||
import rolesRoute from "./routes/roles/route";
|
||||
import { logger } from "hono/logger";
|
||||
import DashboardError from "./errors/DashboardError";
|
||||
|
||||
configDotenv();
|
||||
|
||||
|
|
@ -20,19 +21,6 @@ export type HonoVariables = {
|
|||
|
||||
const app = new Hono<{ Variables: HonoVariables }>();
|
||||
|
||||
// app.use(async (c, next) => {
|
||||
// const authHeader = c.req.header("Authorization");
|
||||
|
||||
// if (authHeader && authHeader.startsWith("Bearer ")) {
|
||||
// const token = authHeader.substring(7);
|
||||
// const payload = await verifyAccessToken(token);
|
||||
|
||||
// if (payload) c.set("uid", payload.uid);
|
||||
// }
|
||||
|
||||
// await next();
|
||||
// });
|
||||
|
||||
const routes = app
|
||||
.use(logger())
|
||||
.use(
|
||||
|
|
@ -91,6 +79,16 @@ const routes = app
|
|||
.route("/dashboard", dashboardRoutes)
|
||||
.route("/roles", rolesRoute)
|
||||
.onError((err, c) => {
|
||||
if (err instanceof DashboardError) {
|
||||
return c.json(
|
||||
{
|
||||
message: err.message,
|
||||
errorCode: err.errorCode,
|
||||
formErrors: err.formErrors,
|
||||
},
|
||||
err.statusCode
|
||||
);
|
||||
}
|
||||
if (err instanceof HTTPException) {
|
||||
console.log(err);
|
||||
return c.json(
|
||||
|
|
|
|||
11
apps/backend/src/types/DashboardErrorParameter.d.ts
vendored
Normal file
11
apps/backend/src/types/DashboardErrorParameter.d.ts
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import { StatusCode } from "hono/utils/http-status";
|
||||
|
||||
interface DashboardErrorParameter {
|
||||
errorCode: string;
|
||||
statusCode?: StatusCode;
|
||||
message: string;
|
||||
formErrors?: Record<string, string>;
|
||||
severity?: "CRITICAL" | "HIGH" | "MEDIUM" | "LOW";
|
||||
}
|
||||
|
||||
export default DashboardErrorParameter;
|
||||
Loading…
Reference in New Issue
Block a user