-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathon-error.ts
31 lines (27 loc) · 875 Bytes
/
on-error.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { Elysia } from "elysia";
import { ElysiaLogging } from "../src/elysiaLogging";
import { type Logger, LogFormat } from "../src/types";
// Use console for logging
const logger : Logger = console;
// Create ElysiaLogging instance
const elysiaLogging = ElysiaLogging(logger, {
format: LogFormat.SHORT
});
// Create Elysia app
const app = new Elysia()
.use(elysiaLogging)
.onError(({error}) => {
// Log error message, if it has one, otherwise log the error object
logger.error(error?.message ?? error);
})
.get("/", () => {
if (Math.random() < 0.75) {
return new Response("Welcome to Bun!");
}
throw { message: 'Whoops!', name: 'CustomError' };
})
.listen({
port: Bun.env.PORT ?? 3000,
maxRequestBodySize: Number.MAX_SAFE_INTEGER,
});
logger.info(`🦊 Running at http://${app.server?.hostname}:${app.server?.port}`);