@@ -9,6 +9,7 @@ import { paymentAmountFromNumber, WalletCurrency } from "@domain/shared"
9
9
import {
10
10
DealerStalePriceError ,
11
11
NoConnectionToDealerError ,
12
+ NoDealerPriceDataAvailableError ,
12
13
UnknownDealerPriceServiceError ,
13
14
} from "@domain/dealer-price"
14
15
@@ -109,7 +110,7 @@ export const DealerPriceService = (
109
110
return paymentAmountFromNumber ( { amount, currency : WalletCurrency . Usd } )
110
111
} catch ( error ) {
111
112
baseLogger . error ( { error } , "GetCentsFromSatsForImmediateBuy unable to fetch price" )
112
- return parseDealerErrors ( error )
113
+ return handleDealerErrors ( error )
113
114
}
114
115
}
115
116
@@ -129,7 +130,7 @@ export const DealerPriceService = (
129
130
{ error } ,
130
131
"GetCentsFromSatsForImmediateSell unable to fetch price" ,
131
132
)
132
- return parseDealerErrors ( error )
133
+ return handleDealerErrors ( error )
133
134
}
134
135
}
135
136
@@ -146,7 +147,7 @@ export const DealerPriceService = (
146
147
return paymentAmountFromNumber ( { amount, currency : WalletCurrency . Usd } )
147
148
} catch ( error ) {
148
149
baseLogger . error ( { error } , "GetCentsFromSatsForFutureBuy unable to fetch price" )
149
- return parseDealerErrors ( error )
150
+ return handleDealerErrors ( error )
150
151
}
151
152
}
152
153
@@ -163,7 +164,7 @@ export const DealerPriceService = (
163
164
return paymentAmountFromNumber ( { amount, currency : WalletCurrency . Usd } )
164
165
} catch ( error ) {
165
166
baseLogger . error ( { error } , "GetCentsFromSatsForFutureSell unable to fetch price" )
166
- return parseDealerErrors ( error )
167
+ return handleDealerErrors ( error )
167
168
}
168
169
}
169
170
@@ -180,7 +181,7 @@ export const DealerPriceService = (
180
181
return paymentAmountFromNumber ( { amount, currency : WalletCurrency . Btc } )
181
182
} catch ( error ) {
182
183
baseLogger . error ( { error } , "GetSatsFromCentsForImmediateBuy unable to fetch price" )
183
- return parseDealerErrors ( error )
184
+ return handleDealerErrors ( error )
184
185
}
185
186
}
186
187
@@ -200,7 +201,7 @@ export const DealerPriceService = (
200
201
{ error } ,
201
202
"GetSatsFromCentsForImmediateSell unable to fetch price" ,
202
203
)
203
- return parseDealerErrors ( error )
204
+ return handleDealerErrors ( error )
204
205
}
205
206
}
206
207
@@ -217,7 +218,7 @@ export const DealerPriceService = (
217
218
return paymentAmountFromNumber ( { amount, currency : WalletCurrency . Btc } )
218
219
} catch ( error ) {
219
220
baseLogger . error ( { error } , "GetSatsFromCentsForFutureBuy unable to fetch price" )
220
- return parseDealerErrors ( error )
221
+ return handleDealerErrors ( error )
221
222
}
222
223
}
223
224
@@ -234,7 +235,7 @@ export const DealerPriceService = (
234
235
return paymentAmountFromNumber ( { amount, currency : WalletCurrency . Btc } )
235
236
} catch ( error ) {
236
237
baseLogger . error ( { error } , "GetSatsFromCentsForFutureSell unable to fetch price" )
237
- return parseDealerErrors ( error )
238
+ return handleDealerErrors ( error )
238
239
}
239
240
}
240
241
@@ -248,7 +249,7 @@ export const DealerPriceService = (
248
249
return toWalletPriceRatio ( response . getRatioInCentsPerSatoshis ( ) )
249
250
} catch ( error ) {
250
251
baseLogger . error ( { error } , "GetCentsPerSatsExchangeMidRate unable to fetch price" )
251
- return parseDealerErrors ( error )
252
+ return handleDealerErrors ( error )
252
253
}
253
254
}
254
255
@@ -272,15 +273,28 @@ export const DealerPriceService = (
272
273
} )
273
274
}
274
275
275
- /* eslint @typescript-eslint/ban-ts-comment: "off" */
276
- // @ts -ignore-next-line no-implicit-any error
277
- const parseDealerErrors = ( error ) : DealerPriceServiceError => {
278
- if ( error . details === "No connection established" ) {
279
- return new NoConnectionToDealerError ( )
280
- }
281
- if ( error . message . includes ( "StalePrice" ) ) {
282
- return new DealerStalePriceError ( )
283
- }
276
+ const handleDealerErrors = ( err : Error | string ) => {
277
+ const errMsg = typeof err === "string" ? err : err . message
278
+
279
+ const match = ( knownErrDetail : RegExp ) : boolean => knownErrDetail . test ( errMsg )
280
+
281
+ switch ( true ) {
282
+ case match ( KnownDealerErrorDetails . NoConnection ) :
283
+ return new NoConnectionToDealerError ( errMsg )
284
284
285
- return new UnknownDealerPriceServiceError ( error . message )
285
+ case match ( KnownDealerErrorDetails . StalePrice ) :
286
+ return new DealerStalePriceError ( errMsg )
287
+
288
+ case match ( KnownDealerErrorDetails . NoPriceData ) :
289
+ return new NoDealerPriceDataAvailableError ( errMsg )
290
+
291
+ default :
292
+ return new UnknownDealerPriceServiceError ( errMsg )
293
+ }
286
294
}
295
+
296
+ export const KnownDealerErrorDetails = {
297
+ NoConnection : / N o c o n n e c t i o n e s t a b l i s h e d / ,
298
+ StalePrice : / S t a l e P r i c e / ,
299
+ NoPriceData : / N o p r i c e d a t a a v a i l a b l e / ,
300
+ } as const
0 commit comments