-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetTransactionError.ts
48 lines (44 loc) · 1.43 KB
/
getTransactionError.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import type { Account } from '../../accounts/types.js'
import type { SendTransactionParameters } from '../../actions/wallet/sendTransaction.js'
import type { BaseError } from '../../errors/base.js'
import { UnknownNodeError } from '../../errors/node.js'
import {
TransactionExecutionError,
type TransactionExecutionErrorType,
} from '../../errors/transaction.js'
import type { ErrorType } from '../../errors/utils.js'
import type { Chain } from '../../types/chain.js'
import {
type GetNodeErrorParameters,
type GetNodeErrorReturnType,
getNodeError,
} from './getNodeError.js'
export type GetTransactionErrorParameters = Omit<
SendTransactionParameters,
'account' | 'chain'
> & {
account: Account
chain?: Chain | undefined
docsPath?: string | undefined
}
export type GetTransactionErrorReturnType<cause = ErrorType> = Omit<
TransactionExecutionErrorType,
'cause'
> & { cause: cause | GetNodeErrorReturnType }
export function getTransactionError<err extends ErrorType<string>>(
err: err,
{ docsPath, ...args }: GetTransactionErrorParameters,
): GetTransactionErrorReturnType<err> {
const cause = (() => {
const cause = getNodeError(
err as {} as BaseError,
args as GetNodeErrorParameters,
)
if (cause instanceof UnknownNodeError) return err as {} as BaseError
return cause
})()
return new TransactionExecutionError(cause, {
docsPath,
...args,
}) as GetTransactionErrorReturnType<err>
}