fix(api): add global error handler and export app type
- Add .onError hook for centralized error handling and consistent JSON formatting - Export app instance and App type for Eden Treaty client integration Reference: Task 2, Issues 1 and 2
This commit is contained in:
@@ -1,7 +1,16 @@
|
|||||||
import { Elysia } from "elysia"
|
import { Elysia } from "elysia"
|
||||||
import { healthRoutes } from "@/lib/api/health"
|
import { healthRoutes } from "@/lib/api/health"
|
||||||
|
|
||||||
const app = new Elysia({ prefix: "/api" })
|
export const app = new Elysia({ prefix: "/api" })
|
||||||
|
.onError(({ code, error, set, request }) => {
|
||||||
|
console.error(`[API Error] ${code} ${request.url}`,
|
||||||
|
error instanceof Error ? error.message : error
|
||||||
|
)
|
||||||
|
set.status = code === "NOT_FOUND" ? 404 : 500
|
||||||
|
return {
|
||||||
|
error: code === "NOT_FOUND" ? "Not found" : "Internal server error",
|
||||||
|
}
|
||||||
|
})
|
||||||
.use(healthRoutes)
|
.use(healthRoutes)
|
||||||
.get("/", () => ({
|
.get("/", () => ({
|
||||||
name: "DeckyVault API",
|
name: "DeckyVault API",
|
||||||
@@ -13,3 +22,5 @@ export const POST = app.fetch
|
|||||||
export const PUT = app.fetch
|
export const PUT = app.fetch
|
||||||
export const DELETE = app.fetch
|
export const DELETE = app.fetch
|
||||||
export const PATCH = app.fetch
|
export const PATCH = app.fetch
|
||||||
|
|
||||||
|
export type App = typeof app
|
||||||
|
|||||||
Reference in New Issue
Block a user