/
ErrorHandler.ts
68 lines (62 loc) · 2.15 KB
/
ErrorHandler.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Copyright (c) 2018-2023, BOTLabs GmbH.
*
* This source code is licensed under the BSD 4-Clause "Original" license
* found in the LICENSE file in the root directory of this source tree.
*/
/**
* ErrorHandler helps spot and determine transaction errors.
*
* @packageDocumentation
*/
import type { DispatchError } from '@polkadot/types/interfaces'
import type { ISubmittableResult } from '@kiltprotocol/types'
import type { RegistryError } from '@polkadot/types/types'
/**
* Checks if there is `SystemEvent.ExtrinsicFailed` in the list of
* transaction events within the given `extrinsicResult`.
*
* @param extrinsicResult The result of a submission.
* @returns Whether the extrinsic submission failed.
*/
export function extrinsicFailed(extrinsicResult: ISubmittableResult): boolean {
return extrinsicResult.events.some((eventRecord) => {
const { section, method } = eventRecord.event
return section === 'system' && method === 'ExtrinsicFailed' // as done in https://github.com/polkadot-js/apps/blob/51835328db5f0eb90a9efcc7bf5510704a7ab279/packages/react-components/src/Status/Queue.tsx
})
}
/**
* Checks if there is `SystemEvent.ExtrinsicSuccess` in the list of
* transaction events within the given `extrinsicResult`.
*
* @param extrinsicResult The result of a submission.
* @returns Whether the extrinsic submission succeeded.
*/
export function extrinsicSuccessful(
extrinsicResult: ISubmittableResult
): boolean {
return extrinsicResult.events.some((eventRecord) => {
const { section, method } = eventRecord.event
return section === 'system' && method === 'ExtrinsicSuccess'
})
}
/**
* Get the extrinsic error from the transaction result.
*
* @param extrinsicResult The transaction result.
* @returns The extrinsic error.
*/
export function getExtrinsicError(
extrinsicResult: ISubmittableResult
): RegistryError | DispatchError | null {
const errorEvent = extrinsicResult.dispatchError
if (errorEvent && errorEvent.isModule) {
const moduleError = errorEvent.asModule
try {
return moduleError.registry.findMetaError(moduleError)
} catch {
// handled with last return
}
}
return errorEvent || null
}