1 |
|
2 |
|
3 |
|
4 |
|
5 | export const ERROR_CODES = {
|
6 | MISSING_PARAMETER: 'missing_parameter',
|
7 | REMOTE_SERVICE_ERROR: 'remote_service_error',
|
8 | INVALID_STATE: 'invalid_state',
|
9 | NO_SESSION_DATA: 'no_session_data',
|
10 | UNKNOWN: 'unknown'
|
11 | }
|
12 |
|
13 | Object.freeze(ERROR_CODES)
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | type ErrorType = {
|
19 | code: string,
|
20 | parameter?: string,
|
21 | message: string
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | export class BlockstackError extends Error {
|
28 | message: string
|
29 |
|
30 | code: string
|
31 |
|
32 | parameter?: string
|
33 |
|
34 | constructor(error: ErrorType) {
|
35 | super(error.message)
|
36 | this.message = error.message
|
37 | this.code = error.code
|
38 | this.parameter = error.parameter ? error.parameter : null
|
39 | }
|
40 |
|
41 | toString() {
|
42 | return `${super.toString()}
|
43 | code: ${this.code} param: ${this.parameter ? this.parameter : 'n/a'}`
|
44 | }
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | export class FileNotFound extends BlockstackError {
|
51 | constructor(message: string) {
|
52 | super({ message, code: 'file_not_found' })
|
53 | this.name = 'FileNotFound'
|
54 | }
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | export class InvalidParameterError extends BlockstackError {
|
61 | constructor(parameter: string, message: string = '') {
|
62 | super({ code: 'missing_parameter', message, parameter: '' })
|
63 | this.name = 'MissingParametersError'
|
64 | }
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | export class MissingParameterError extends BlockstackError {
|
71 | constructor(parameter: string, message: string = '') {
|
72 | super({ code: ERROR_CODES.MISSING_PARAMETER, message, parameter })
|
73 | this.name = 'MissingParametersError'
|
74 | }
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | export class RemoteServiceError extends BlockstackError {
|
81 | response: Response
|
82 |
|
83 | constructor(response: Response, message: string = '') {
|
84 | super({ code: ERROR_CODES.REMOTE_SERVICE_ERROR, message })
|
85 | this.response = response
|
86 | }
|
87 | }
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | export class InvalidDIDError extends BlockstackError {
|
93 | constructor(message: string = '') {
|
94 | super({ code: 'invalid_did_error', message })
|
95 | this.name = 'InvalidDIDError'
|
96 | }
|
97 | }
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | export class NotEnoughFundsError extends BlockstackError {
|
103 | leftToFund: number
|
104 |
|
105 | constructor(leftToFund: number) {
|
106 | const message = `Not enough UTXOs to fund. Left to fund: ${leftToFund}`
|
107 | super({ code: 'not_enough_error', message })
|
108 | this.leftToFund = leftToFund
|
109 | this.name = 'NotEnoughFundsError'
|
110 | this.message = message
|
111 | }
|
112 | }
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | export class InvalidAmountError extends BlockstackError {
|
119 | fees: number
|
120 |
|
121 | specifiedAmount: number
|
122 |
|
123 | constructor(fees: number, specifiedAmount: number) {
|
124 | const message = `Not enough coin to fund fees transaction fees. Fees would be ${fees},`
|
125 | + ` specified spend is ${specifiedAmount}`
|
126 | super({ code: 'invalid_amount_error', message })
|
127 | this.specifiedAmount = specifiedAmount
|
128 | this.fees = fees
|
129 | this.name = 'InvalidAmountError'
|
130 | this.message = message
|
131 | }
|
132 | }
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | export class LoginFailedError extends BlockstackError {
|
138 | constructor(reason: string) {
|
139 | const message = `Failed to login: ${reason}`
|
140 | super({ code: 'login_failed', message })
|
141 | this.message = message
|
142 | this.name = 'LoginFailedError'
|
143 | }
|
144 | }
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | export class SignatureVerificationError extends BlockstackError {
|
150 | constructor(reason: string) {
|
151 | const message = `Failed to verify signature: ${reason}`
|
152 | super({ code: 'signature_verification_failure', message })
|
153 | this.message = message
|
154 | this.name = 'SignatureVerificationError'
|
155 | }
|
156 | }
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | export class InvalidStateError extends BlockstackError {
|
162 | constructor(message: string) {
|
163 | super({ code: ERROR_CODES.INVALID_STATE, message })
|
164 | this.message = message
|
165 | this.name = 'InvalidStateError'
|
166 | }
|
167 | }
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | export class NoSessionDataError extends BlockstackError {
|
173 | constructor(message: string) {
|
174 | super({ code: ERROR_CODES.INVALID_STATE, message })
|
175 | this.message = message
|
176 | this.name = 'NoSessionDataError'
|
177 | }
|
178 | }
|