Skip to content
Myr

fix(types): correct types for `app.notFound` / `app.onError` (#512)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix #511

authored by Yusuke Wada · committed by GitHub · parent 5a642ed · Browse files · 6 files+26 added−26 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+26 added−26 removed

  • other:+26 added−26 removed

6 files+26 added−26 removed

modified:deno_dist/compose.ts+3 added-3 removed
modified:deno_dist/context.ts+2 added-2 removed
57 unmodified lines
58 private _prettySpace: number = 259 private _map: Record<string, any> | undefined60 private _headers: Record<string, string> | undefined61 private _res: Response | undefined62 private notFoundHandler: NotFoundHandler62 private notFoundHandler: NotFoundHandler<E>6364 constructor(65 req: Request,66 env: E['Bindings'] | undefined = undefined,67 executionCtx: FetchEvent | ExecutionContext | undefined = undefined,68 notFoundHandler: NotFoundHandler = () => new Response()68 notFoundHandler: NotFoundHandler<E> = () => new Response()69 ) {70 this._executionCtx = executionCtx71 this.req = req72 this.env = env || ({} as Bindings)
120 unmodified lines
modified:deno_dist/hono.ts+8 added-8 removed
122 unmodified lines
123124 Object.assign(this, init)125 }126127 private notFoundHandler: NotFoundHandler = (c: Context) => {127 private notFoundHandler: NotFoundHandler<E> = (c: Context<string, E>) => {128 const message = defaultNotFoundMessage129 return c.text(message, 404)130 }131132 private errorHandler: ErrorHandler = (err: Error, c: Context) => {132 private errorHandler: ErrorHandler<E> = (err: Error, c: Context<string, E>) => {133 console.error(`${err.stack || err.message}`)134 const message = 'Internal Server Error'135 return c.text(message, 500)136 }
28 unmodified lines
165 })166 return this167 }168169 onError(handler: ErrorHandler): Hono<E, P> {169 onError(handler: ErrorHandler<E>): Hono<E, P> {170 this.errorHandler = handler171 return this172 }173174 notFound(handler: NotFoundHandler): Hono<E, P> {174 notFound(handler: NotFoundHandler<E>): Hono<E, P> {175 this.notFoundHandler = handler176 return this177 }178
32 unmodified lines
211 if (res) {212 const awaited = res instanceof Promise ? await res : res213 if (awaited) return awaited214 }215 return this.notFoundHandler(c as Context)215 return this.notFoundHandler(c)216 } catch (err) {217 if (err instanceof Error) {218 return this.errorHandler(err, c as Context)218 return this.errorHandler(err, c)219 }220 throw err221 }222 }223224 const handlers = result ? result.handlers : [this.notFoundHandler]225226 const composed = compose<HonoContext<string, E>>(handlers, this.notFoundHandler)226 const composed = compose<HonoContext<string, E>, E>(handlers, this.notFoundHandler)227 let context: HonoContext<string, E>228 try {229 const tmp = composed(c)230 context = tmp instanceof Promise ? await tmp : tmp
3 unmodified lines
234 )235 }236 } catch (err) {237 if (err instanceof Error) {238 return this.errorHandler(err, c as Context)238 return this.errorHandler(err, c)239 }240 throw err241 }242
16 unmodified lines
modified:src/compose.ts+3 added-3 removed
modified:src/context.ts+2 added-2 removed
57 unmodified lines
58 private _prettySpace: number = 259 private _map: Record<string, any> | undefined60 private _headers: Record<string, string> | undefined61 private _res: Response | undefined62 private notFoundHandler: NotFoundHandler62 private notFoundHandler: NotFoundHandler<E>6364 constructor(65 req: Request,66 env: E['Bindings'] | undefined = undefined,67 executionCtx: FetchEvent | ExecutionContext | undefined = undefined,68 notFoundHandler: NotFoundHandler = () => new Response()68 notFoundHandler: NotFoundHandler<E> = () => new Response()69 ) {70 this._executionCtx = executionCtx71 this.req = req72 this.env = env || ({} as Bindings)
120 unmodified lines
modified:src/hono.ts+8 added-8 removed
122 unmodified lines
123124 Object.assign(this, init)125 }126127 private notFoundHandler: NotFoundHandler = (c: Context) => {127 private notFoundHandler: NotFoundHandler<E> = (c: Context<string, E>) => {128 const message = defaultNotFoundMessage129 return c.text(message, 404)130 }131132 private errorHandler: ErrorHandler = (err: Error, c: Context) => {132 private errorHandler: ErrorHandler<E> = (err: Error, c: Context<string, E>) => {133 console.error(`${err.stack || err.message}`)134 const message = 'Internal Server Error'135 return c.text(message, 500)136 }
28 unmodified lines
165 })166 return this167 }168169 onError(handler: ErrorHandler): Hono<E, P> {169 onError(handler: ErrorHandler<E>): Hono<E, P> {170 this.errorHandler = handler171 return this172 }173174 notFound(handler: NotFoundHandler): Hono<E, P> {174 notFound(handler: NotFoundHandler<E>): Hono<E, P> {175 this.notFoundHandler = handler176 return this177 }178
32 unmodified lines
211 if (res) {212 const awaited = res instanceof Promise ? await res : res213 if (awaited) return awaited214 }215 return this.notFoundHandler(c as Context)215 return this.notFoundHandler(c)216 } catch (err) {217 if (err instanceof Error) {218 return this.errorHandler(err, c as Context)218 return this.errorHandler(err, c)219 }220 throw err221 }222 }223224 const handlers = result ? result.handlers : [this.notFoundHandler]225226 const composed = compose<HonoContext<string, E>>(handlers, this.notFoundHandler)226 const composed = compose<HonoContext<string, E>, E>(handlers, this.notFoundHandler)227 let context: HonoContext<string, E>228 try {229 const tmp = composed(c)230 context = tmp instanceof Promise ? await tmp : tmp
3 unmodified lines
234 )235 }236 } catch (err) {237 if (err instanceof Error) {238 return this.errorHandler(err, c as Context)238 return this.errorHandler(err, c)239 }240 throw err241 }242
16 unmodified lines