Skip to content

Commit

Permalink
fix(createError): preserve original error stack (#161)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <[email protected]>
  • Loading branch information
danielroe and pi0 authored Aug 10, 2022
1 parent ccc9c7e commit 8213421
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ export function createError (input: string | Partial<H3Error>): H3Error {

const err = new H3Error(input.message ?? input.statusMessage, input.cause ? { cause: input.cause } : undefined)

if ('stack' in input) {
try {
Object.defineProperty(err, 'stack', { get () { return input.stack } })
} catch {
try { err.stack = input.stack } catch {}
}
}

if (input.statusCode) { err.statusCode = input.statusCode }
if (input.statusMessage) { err.statusMessage = input.statusMessage }
if (input.data) { err.data = input.data }
Expand Down

0 comments on commit 8213421

Please sign in to comment.