Skip to content

Commit

Permalink
fix: propagate message + cause to Error instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Jul 29, 2024
1 parent b5b6032 commit 5a528d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-cups-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Propagated `message` + `cause` to root `Error` instance.
13 changes: 7 additions & 6 deletions src/errors/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ export class BaseError extends Error {
docsPath?: string | undefined
metaMessages?: string[] | undefined
shortMessage: string
version: string

override name = 'ViemError'
version = getVersion()

constructor(shortMessage: string, args: BaseErrorParameters = {}) {
super()

const details =
args.cause instanceof BaseError
? args.cause.details
Expand All @@ -32,8 +30,9 @@ export class BaseError extends Error {
args.cause instanceof BaseError
? args.cause.docsPath || args.docsPath
: args.docsPath
const version = getVersion()

this.message = [
const message = [
shortMessage || 'An error occurred.',
'',
...(args.metaMessages ? [...args.metaMessages, ''] : []),
Expand All @@ -45,14 +44,16 @@ export class BaseError extends Error {
]
: []),
...(details ? [`Details: ${details}`] : []),
`Version: ${this.version}`,
`Version: ${version}`,
].join('\n')

if (args.cause) this.cause = args.cause
super(message, args.cause ? { cause: args.cause } : undefined)

this.details = details
this.docsPath = docsPath
this.metaMessages = args.metaMessages
this.shortMessage = shortMessage
this.version = version
}

walk(): Error
Expand Down

0 comments on commit 5a528d9

Please sign in to comment.