1
1
import { CSL , Cardano , cslToCore } from '@cardano-sdk/core' ;
2
2
import { OutsideOfValidityInterval } from '@cardano-ogmios/schema' ;
3
- import { fromSerializableObject , toSerializableObject } from '@cardano-sdk/util' ;
4
-
5
- export interface DeserializedError {
6
- innerError ?: {
7
- name : string ;
8
- message : string ;
9
- } ;
10
- name : string ;
11
- message : string ;
12
- }
13
-
14
- export interface SerializedError {
15
- value : {
16
- innerError ?: {
17
- name : string ;
18
- message : string ;
19
- stack ?: string ;
20
- } ;
21
- name : string ;
22
- message : string ;
23
- stack ?: string ;
24
- } ;
25
- }
3
+ import { toSerializableObject } from '@cardano-sdk/util' ;
26
4
27
5
export const TX_SUBMISSION_QUEUE = 'cardano-tx-submit' ;
28
6
29
7
/**
30
- * Enriches an error with the right prototype
8
+ * Analyzes a serializable error to get the right prototype object
31
9
*
32
- * @param err the error to enrich
10
+ * @param error the error to analyze
11
+ * @returns the right prototype for the error
33
12
*/
34
- const addPrototypeToError = ( err : DeserializedError ) => {
35
- const { name, innerError } = err ;
36
- let prototype : object | null = Error . prototype ;
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ export const getErrorPrototype = ( error : unknown ) => {
15
+ if ( typeof error === 'object' ) {
16
+ const rawError = error as Cardano . TxSubmissionError ;
37
17
38
- if ( name in Cardano . TxSubmissionErrors )
39
- prototype = Cardano . TxSubmissionErrors [ name as keyof typeof Cardano . TxSubmissionErrors ] . prototype ;
40
-
41
- Object . setPrototypeOf ( err , prototype ) ;
42
-
43
- if ( innerError ) addPrototypeToError ( innerError ) ;
44
- } ;
18
+ if ( typeof rawError . name === 'string' && typeof rawError . message === 'string' ) {
19
+ const txSubmissionErrorName = rawError . name as keyof typeof Cardano . TxSubmissionErrors ;
20
+ const ErrorClass = Cardano . TxSubmissionErrors [ txSubmissionErrorName ] ;
45
21
46
- /**
47
- * Deserializes a serialized error
48
- *
49
- * @param serializedError the error to deserialize
50
- */
51
- export const deserializeError = ( serializedError : SerializedError ) : unknown => {
52
- const ret = fromSerializableObject ( serializedError ) as DeserializedError ;
53
-
54
- addPrototypeToError ( ret ) ;
22
+ if ( ErrorClass ) return ErrorClass . prototype ;
23
+ }
24
+ }
55
25
56
- return ret ;
26
+ return Error . prototype ;
57
27
} ;
58
28
59
29
/**
@@ -63,38 +33,16 @@ export const deserializeError = (serializedError: SerializedError): unknown => {
63
33
*/
64
34
export const serializeError = ( err : unknown ) => {
65
35
let isRetriable = false ;
66
- let serializedError : SerializedError | null = null ;
67
-
68
- const lastChance = ( ) => {
69
- try {
70
- serializedError = { value : { message : JSON . stringify ( err ) , name : 'Error' } } ;
71
- } catch {
72
- serializedError = { value : { message : 'Unknown' , name : 'Error' } } ;
73
- }
74
- } ;
75
-
76
- try {
77
- serializedError = toSerializableObject ( err ) as SerializedError ;
78
- } catch {
79
- lastChance ( ) ;
80
- }
81
36
82
- if ( ! serializedError ! . value ) lastChance ( ) ;
37
+ const serializableError = toSerializableObject ( err ) ;
83
38
84
- delete serializedError ! . value . stack ;
85
- if ( serializedError ! . value . innerError ) delete serializedError ! . value . innerError . stack ;
39
+ if ( err instanceof Cardano . TxSubmissionErrors . OutsideOfValidityIntervalError ) {
40
+ const details = JSON . parse ( err . message ) as OutsideOfValidityInterval [ 'outsideOfValidityInterval' ] ;
86
41
87
- if ( serializedError ! . value . name === 'OutsideOfValidityIntervalError' )
88
- try {
89
- const details = JSON . parse (
90
- serializedError ! . value . message
91
- ) as OutsideOfValidityInterval [ 'outsideOfValidityInterval' ] ;
92
-
93
- if ( details . interval . invalidBefore && details . currentSlot <= details . interval . invalidBefore ) isRetriable = true ;
94
- // eslint-disable-next-line no-empty
95
- } catch { }
42
+ if ( details . interval . invalidBefore && details . currentSlot <= details . interval . invalidBefore ) isRetriable = true ;
43
+ }
96
44
97
- return { isRetriable, serializedError } ;
45
+ return { isRetriable, serializableError } ;
98
46
} ;
99
47
100
48
export const txBodyToId = ( txBody : Buffer | Uint8Array , dummy ?: boolean ) => {
0 commit comments