add forwarder error intance

This commit is contained in:
rafiarrafif
2025-05-27 02:47:29 +07:00
parent ee8e8d08db
commit a7c984b316
8 changed files with 51 additions and 18 deletions

View File

@ -0,0 +1,13 @@
import { AppError } from "./app";
export function ErrorForwarder(
statusCode: number,
message: string,
cause: unknown
): never {
if (cause instanceof AppError) {
throw cause;
}
throw new AppError(statusCode, message, cause);
}

View File

@ -35,7 +35,7 @@ export const authVerificationService = async (cookie: string) => {
1000 1000
); );
await storeUserSessionToCacheService(sessionCheckOnDB, timeExpires); await storeUserSessionToCacheService(sessionCheckOnDB, timeExpires);
return "daridb"; return sessionCheckOnDB;
} }
} else { } else {
return jwtSession; return jwtSession;

View File

@ -1,13 +1,15 @@
import { Context } from "elysia"; import { Context } from "elysia";
import { mainErrorHandler } from "../../helpers/error/handler"; import { mainErrorHandler } from "../../helpers/error/handler";
import { debugService } from "./debug.service"; import { debugService } from "./debug.service";
import { returnWriteResponse } from "../../helpers/callback/httpResponse";
export const debugController = (ctx: Context) => { export const debugController = async (ctx: Context) => {
ctx.set.status = 418; try {
return Math.floor( const dataFromService = await debugService();
(new Date("2025-07-13 16:22:12.335").getTime() - new Date().getTime()) / return returnWriteResponse(ctx.set, 200, "Message Sent", dataFromService);
1000 } catch (error) {
); return mainErrorHandler(ctx.set, error);
}
}; };
// buat debug untuk date to number (second) // buat debug untuk date to number (second)

View File

@ -1,6 +1,11 @@
import { AppError } from "../../helpers/error/instances/app"; import { ErrorForwarder } from "../../helpers/error/instances/forwarder";
import { debug2Service } from "./debug2.service";
export const debugService = () => { export const debugService = async () => {
// return "OK2"; try {
throw new AppError(404, "not found"); const dataFromService = await debug2Service();
return dataFromService;
} catch (error) {
ErrorForwarder(402, "Error from 1", error);
}
}; };

View File

@ -0,0 +1,11 @@
import { ErrorForwarder } from "../../helpers/error/instances/forwarder";
import { debug3Service } from "./debug3.service";
export const debug2Service = async () => {
try {
const dataFromService = await debug3Service();
return dataFromService;
} catch (error) {
ErrorForwarder(402, "Error from 2", error);
}
};

View File

@ -0,0 +1,5 @@
import { AppError } from "../../helpers/error/instances/app";
export const debug3Service = async () => {
throw new AppError(402, "Error from 3");
};

View File

@ -8,6 +8,6 @@ export const checkUserSessionInCacheRepo = async (redisKeyName: string) => {
return userSessionInRedis; return userSessionInRedis;
} catch (error) { } catch (error) {
throw new AppError(500, "Server cache error"); throw new AppError(500, "Server cache error", error);
} }
}; };

View File

@ -6,10 +6,7 @@ export const checkUserSessionInCacheService = async (
sessionId: string sessionId: string
) => { ) => {
const redisKeyName = `${process.env.app_name}:users:${userId}:sessions:${sessionId}`; const redisKeyName = `${process.env.app_name}:users:${userId}:sessions:${sessionId}`;
try {
const userSessionInRedis = await checkUserSessionInCacheRepo(redisKeyName); const userSessionInRedis = await checkUserSessionInCacheRepo(redisKeyName);
return userSessionInRedis; return userSessionInRedis;
} catch {
throw new AppError(502, "Server cache error");
}
}; };