11import { GraphQLErrorCode } from '../../graphql/errors'
22
3- export enum QuoteError {
3+ export class QuoteError extends Error {
4+ public type : QuoteErrorCode
5+ public details ?: Record < string , unknown >
6+
7+ constructor ( type : QuoteErrorCode , details ?: Record < string , unknown > ) {
8+ super ( errorToMessage [ type ] , details )
9+ this . type = type
10+ this . details = details
11+ }
12+ }
13+
14+ export enum QuoteErrorCode {
415 UnknownWalletAddress = 'UnknownWalletAddress' ,
516 InvalidAmount = 'InvalidAmount' ,
617 InvalidReceiver = 'InvalidReceiver' ,
@@ -9,35 +20,34 @@ export enum QuoteError {
920}
1021
1122// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
12- export const isQuoteError = ( o : any ) : o is QuoteError =>
13- Object . values ( QuoteError ) . includes ( o )
23+ export const isQuoteError = ( o : any ) : o is QuoteError => o instanceof QuoteError
1424
1525export const errorToHTTPCode : {
16- [ key in QuoteError ] : number
26+ [ key in QuoteErrorCode ] : number
1727} = {
18- [ QuoteError . UnknownWalletAddress ] : 404 ,
19- [ QuoteError . InvalidAmount ] : 400 ,
20- [ QuoteError . InvalidReceiver ] : 400 ,
21- [ QuoteError . InactiveWalletAddress ] : 400 ,
22- [ QuoteError . NonPositiveReceiveAmount ] : 400
28+ [ QuoteErrorCode . UnknownWalletAddress ] : 404 ,
29+ [ QuoteErrorCode . InvalidAmount ] : 400 ,
30+ [ QuoteErrorCode . InvalidReceiver ] : 400 ,
31+ [ QuoteErrorCode . InactiveWalletAddress ] : 400 ,
32+ [ QuoteErrorCode . NonPositiveReceiveAmount ] : 400
2333}
2434
2535export const errorToCode : {
26- [ key in QuoteError ] : GraphQLErrorCode
36+ [ key in QuoteErrorCode ] : GraphQLErrorCode
2737} = {
28- [ QuoteError . UnknownWalletAddress ] : GraphQLErrorCode . NotFound ,
29- [ QuoteError . InvalidAmount ] : GraphQLErrorCode . BadUserInput ,
30- [ QuoteError . InvalidReceiver ] : GraphQLErrorCode . BadUserInput ,
31- [ QuoteError . InactiveWalletAddress ] : GraphQLErrorCode . Inactive ,
32- [ QuoteError . NonPositiveReceiveAmount ] : GraphQLErrorCode . BadUserInput
38+ [ QuoteErrorCode . UnknownWalletAddress ] : GraphQLErrorCode . NotFound ,
39+ [ QuoteErrorCode . InvalidAmount ] : GraphQLErrorCode . BadUserInput ,
40+ [ QuoteErrorCode . InvalidReceiver ] : GraphQLErrorCode . BadUserInput ,
41+ [ QuoteErrorCode . InactiveWalletAddress ] : GraphQLErrorCode . Inactive ,
42+ [ QuoteErrorCode . NonPositiveReceiveAmount ] : GraphQLErrorCode . BadUserInput
3343}
3444
3545export const errorToMessage : {
36- [ key in QuoteError ] : string
46+ [ key in QuoteErrorCode ] : string
3747} = {
38- [ QuoteError . UnknownWalletAddress ] : 'unknown wallet address' ,
39- [ QuoteError . InvalidAmount ] : 'invalid amount' ,
40- [ QuoteError . InvalidReceiver ] : 'invalid receiver' ,
41- [ QuoteError . InactiveWalletAddress ] : 'inactive wallet address' ,
42- [ QuoteError . NonPositiveReceiveAmount ] : 'non-positive receive amount'
48+ [ QuoteErrorCode . UnknownWalletAddress ] : 'unknown wallet address' ,
49+ [ QuoteErrorCode . InvalidAmount ] : 'invalid amount' ,
50+ [ QuoteErrorCode . InvalidReceiver ] : 'invalid receiver' ,
51+ [ QuoteErrorCode . InactiveWalletAddress ] : 'inactive wallet address' ,
52+ [ QuoteErrorCode . NonPositiveReceiveAmount ] : 'non-positive receive amount'
4353}
0 commit comments