Added error class

This commit is contained in:
sianida26 2024-05-06 22:10:59 +07:00
parent 6df91f3adc
commit af2c452e64
3 changed files with 66 additions and 13 deletions

View 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;

View File

@ -11,6 +11,7 @@ import { getSignedCookie } from "hono/cookie";
import dashboardRoutes from "./routes/dashboard/routes"; import dashboardRoutes from "./routes/dashboard/routes";
import rolesRoute from "./routes/roles/route"; import rolesRoute from "./routes/roles/route";
import { logger } from "hono/logger"; import { logger } from "hono/logger";
import DashboardError from "./errors/DashboardError";
configDotenv(); configDotenv();
@ -20,19 +21,6 @@ export type HonoVariables = {
const app = new Hono<{ Variables: 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 const routes = app
.use(logger()) .use(logger())
.use( .use(
@ -91,6 +79,16 @@ const routes = app
.route("/dashboard", dashboardRoutes) .route("/dashboard", dashboardRoutes)
.route("/roles", rolesRoute) .route("/roles", rolesRoute)
.onError((err, c) => { .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) { if (err instanceof HTTPException) {
console.log(err); console.log(err);
return c.json( return c.json(

View 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;