From 30c1f4b894b7dc5f693c7a96d2d660bdc55c2402 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Sun, 30 Nov 2025 10:24:13 -0800 Subject: [PATCH] exception handler --- src/index.tsx | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index c68d6e5..de48bbb 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,6 +1,6 @@ import { join } from 'path' import prettier from 'prettier' -import { type Context, Hono } from 'hono' +import { type Context, Hono, type Schema, type Env } from 'hono' import { serveStatic } from 'hono/bun' import color from 'kleur' @@ -18,14 +18,21 @@ export type HypeProps = { reset?: boolean } -export class Hype extends Hono { +export class Hype< + E extends Env = Env, + S extends Schema = {}, + BasePath extends string = '/' +> extends Hono { + props?: HypeProps routesRegistered = false - props: HypeProps = {} - constructor(props?: HypeProps) { - super() + constructor(props?: HypeProps & ConstructorParameters>[0]) { + super(props) - if (props) this.props = props + if (props) { + const { pico, reset } = props + this.props = { pico, reset } + } } registerRoutes() { @@ -50,6 +57,22 @@ export class Hype extends Hono { console.log(fn(`${c.res.status}`), `${color.bold(method)} ${c.req.url} (${end - start}ms)`) }) + // exception handler + this.onError((err, c) => { + const isDev = process.env.NODE_ENV !== 'production' + + return c.html( + ` + + +

Error: ${err.message}

+ ${isDev ? `
${err.stack}
` : '

An error occurred

'} + + `, + 500 + ) + }) + // prettify HTML output if (process.env.NODE_ENV !== 'production') { this.use('*', async (c, next) => { @@ -102,7 +125,7 @@ export class Hype extends Hono { Layout = (await import(layoutPath + `?t=${Date.now()}`)).default const page = await import(path + `?t=${Date.now()}`) - const innerHTML = typeof page.default === 'function' ? : page.default + const innerHTML = typeof page.default === 'function' ? : page.default const withLayout = Layout ? {innerHTML} : innerHTML return c.html(withLayout) })