Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/adapter/bun/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,24 @@ export const mapCompactResponse = (
}
}

export const errorToResponse = (error: Error, set?: Context['set']) =>
new Response(
export const errorToResponse = (error: Error, set?: Context['set']) => {
// @ts-expect-error
if (typeof error?.toResponse === 'function') {
// @ts-expect-error
const raw = error.toResponse()
const targetSet =
set ?? ({ headers: {}, status: 200, redirect: '' } as Context['set'])
const apply = (resolved: unknown) => {
if (resolved instanceof Response) targetSet.status = resolved.status
return mapResponse(resolved, targetSet)
}

return typeof raw?.then === 'function'
? raw.then(apply)
: apply(raw)
}

return new Response(
JSON.stringify({
name: error?.name,
message: error?.message,
Expand All @@ -539,6 +555,7 @@ export const errorToResponse = (error: Error, set?: Context['set']) =>
headers: set?.headers as any
}
)
}

export const createStaticHandler = (
handle: unknown,
Expand Down
21 changes: 19 additions & 2 deletions src/adapter/web-standard/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,24 @@ export const mapCompactResponse = (
}
}

export const errorToResponse = (error: Error, set?: Context['set']) =>
new Response(
export const errorToResponse = (error: Error, set?: Context['set']) => {
// @ts-expect-error
if (typeof error?.toResponse === 'function') {
// @ts-expect-error
const raw = error.toResponse()
const targetSet =
set ?? ({ headers: {}, status: 200, redirect: '' } as Context['set'])
const apply = (resolved: unknown) => {
if (resolved instanceof Response) targetSet.status = resolved.status
return mapResponse(resolved, targetSet)
}

return typeof raw?.then === 'function'
? raw.then(apply)
: apply(raw)
}

return new Response(
JSON.stringify({
name: error?.name,
message: error?.message,
Expand All @@ -572,6 +588,7 @@ export const errorToResponse = (error: Error, set?: Context['set']) =>
headers: set?.headers as any
}
)
}

export const createStaticHandler = (
handle: unknown,
Expand Down
31 changes: 21 additions & 10 deletions src/compose.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AnyElysia } from './index'

import { Value } from '@sinclair/typebox/value'
import { Value, TransformDecodeError } from '@sinclair/typebox/value'
import {
Kind,
OptionalKind,
Expand Down Expand Up @@ -2520,6 +2520,8 @@ export const composeErrorHandler = (app: AnyElysia) => {
`mapResponse,` +
`ERROR_CODE,` +
`ElysiaCustomStatusResponse,` +
`ValidationError,` +
`TransformDecodeError,` +
allocateIf(`onError,`, app.event.error) +
allocateIf(`afterResponse,`, app.event.afterResponse) +
allocateIf(`trace,`, app.event.trace) +
Expand All @@ -2529,11 +2531,8 @@ export const composeErrorHandler = (app: AnyElysia) => {
adapterVariables +
`}=inject\n`

fnLiteral += `return ${
app.event.error?.find(isAsync) || app.event.mapResponse?.find(isAsync)
? 'async '
: ''
}function(context,error,skipGlobal){`
// Always make error handler async since toResponse() may return promises
fnLiteral += `return async function(context,error,skipGlobal){`

fnLiteral += ''

Expand Down Expand Up @@ -2598,6 +2597,17 @@ export const composeErrorHandler = (app: AnyElysia) => {
const saveResponse =
hasTrace || !!hooks.afterResponse?.length ? 'context.response = ' : ''

fnLiteral +=
`if(typeof error?.toResponse==='function'&&!(error instanceof ValidationError)&&!(error instanceof TransformDecodeError)){` +
`try{` +
`let raw=error.toResponse()\n` +
`if(typeof raw?.then==='function')raw=await raw\n` +
`if(raw instanceof Response)set.status=raw.status\n` +
`context.response=context.responseValue=raw\n` +
`}catch(toResponseError){\n` +
`}\n` +
`}\n`

if (app.event.error)
for (let i = 0; i < app.event.error.length; i++) {
const handler = app.event.error[i]
Expand All @@ -2606,7 +2616,7 @@ export const composeErrorHandler = (app: AnyElysia) => {
isAsync(handler) ? 'await ' : ''
}onError[${i}](context)\n`

fnLiteral += 'if(skipGlobal!==true){'
fnLiteral += 'if(skipGlobal!==true&&!context.response){'

if (hasReturn(handler)) {
fnLiteral +=
Expand Down Expand Up @@ -2652,17 +2662,16 @@ export const composeErrorHandler = (app: AnyElysia) => {
}

fnLiteral +=
`if(error.constructor.name==="ValidationError"||error.constructor.name==="TransformDecodeError"){\n` +
`if(error instanceof ValidationError||error instanceof TransformDecodeError){\n` +
`if(error.error)error=error.error\n` +
`set.status=error.status??422\n` +
afterResponse() +
adapter.validationError +
`\n}\n`

fnLiteral +=
`if(error instanceof Error){` +
`if(!context.response&&error instanceof Error){` +
afterResponse() +
`\nif(typeof error.toResponse==='function')return context.response=context.responseValue=error.toResponse()\n` +
adapter.unknownError +
`\n}`

Expand Down Expand Up @@ -2710,6 +2719,8 @@ export const composeErrorHandler = (app: AnyElysia) => {
mapResponse: app['~adapter'].handler.mapResponse,
ERROR_CODE,
ElysiaCustomStatusResponse,
ValidationError,
TransformDecodeError,
onError: app.event.error?.map(mapFn),
afterResponse: app.event.afterResponse?.map(mapFn),
trace: app.event.trace?.map(mapFn),
Expand Down
31 changes: 30 additions & 1 deletion src/dynamic-handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,23 @@ export const createDynamicErrorHandler = (app: AnyElysia) => {
const errorContext = Object.assign(context, { error, code: error.code })
errorContext.set = context.set

if (app.event.error)
// @ts-expect-error
if (typeof error?.toResponse === 'function' &&
!(error instanceof ValidationError) &&
!(error instanceof TransformDecodeError)) {
try {
// @ts-expect-error
let raw = error.toResponse()
if (typeof raw?.then === 'function') raw = await raw
if (raw instanceof Response) context.set.status = raw.status
context.response = raw
} catch (toResponseError) {
// If toResponse() throws, fall through to normal error handling
// Don't set context.response so onError hooks will run
}
}

if (!context.response && app.event.error)
for (let i = 0; i < app.event.error.length; i++) {
const hook = app.event.error[i]
let response = hook.fn(errorContext as any)
Expand All @@ -696,6 +712,19 @@ export const createDynamicErrorHandler = (app: AnyElysia) => {
))
}

if (context.response) {
if (app.event.mapResponse)
for (let i = 0; i < app.event.mapResponse.length; i++) {
const hook = app.event.mapResponse[i]
let response = hook.fn(errorContext as any)
if (response instanceof Promise) response = await response
if (response !== undefined && response !== null)
context.response = response
}

return mapResponse(context.response, context.set)
}

return new Response(
typeof error.cause === 'string' ? error.cause : error.message,
{
Expand Down
Loading
Loading