decred.org/dcrdex@v1.0.5/client/core/errors.go (about) 1 // This code is available on the terms of the project LICENSE.md file, 2 // also available online at https://blueoakcouncil.org/license/1.0.0. 3 4 package core 5 6 import ( 7 "errors" 8 "fmt" 9 ) 10 11 // Error codes here are used on the frontend. 12 const ( 13 walletErr = iota 14 walletAuthErr 15 noAuthError 16 walletBalanceErr 17 dupeDEXErr 18 assetSupportErr 19 registerErr 20 signatureErr 21 zeroFeeErr 22 feeMismatchErr 23 feeSendErr 24 passwordErr 25 emptyHostErr 26 connectionErr 27 acctKeyErr 28 unknownOrderErr 29 orderParamsErr 30 dbErr 31 authErr 32 connectWalletErr 33 missingWalletErr 34 encryptionErr 35 decodeErr 36 accountVerificationErr 37 accountProofErr 38 parseKeyErr 39 marketErr 40 addressParseErr 41 addrErr 42 fileReadErr 43 unknownDEXErr 44 accountRetrieveErr 45 accountStatusUpdateErr 46 suspendedAcctErr 47 existenceCheckErr 48 createWalletErr 49 activeOrdersErr 50 newAddrErr 51 bondAmtErr 52 bondTimeErr 53 bondAssetErr 54 bondPostErr // TODO 55 ) 56 57 // Error is an error code and a wrapped error. 58 type Error struct { 59 code int 60 err error 61 } 62 63 // Error returns the error string. Satisfies the error interface. 64 func (e *Error) Error() string { 65 return e.err.Error() 66 } 67 68 // Code returns the error code. 69 func (e *Error) Code() *int { 70 return &e.code 71 } 72 73 // Unwrap returns the underlying wrapped error. 74 func (e *Error) Unwrap() error { 75 return e.err 76 } 77 78 // newError is a constructor for a new Error. 79 func newError(code int, s string, a ...any) error { 80 return &Error{ 81 code: code, 82 err: fmt.Errorf(s, a...), // s may contain a %w verb to wrap an error 83 } 84 } 85 86 // codedError converts the error to an Error with the specified code. 87 func codedError(code int, err error) error { 88 return &Error{ 89 code: code, 90 err: err, 91 } 92 } 93 94 // errorHasCode checks whether the error is an Error and has the specified code. 95 func errorHasCode(err error, code int) bool { 96 var e *Error 97 return errors.As(err, &e) && e.code == code 98 } 99 100 // UnwrapErr returns the result of calling the Unwrap method on err, 101 // until it returns a non-wrapped error. 102 func UnwrapErr(err error) error { 103 InnerErr := errors.Unwrap(err) 104 if InnerErr == nil { 105 return err 106 } 107 return UnwrapErr(InnerErr) 108 } 109 110 var ( 111 ErrAccountSuspended = errors.New("may not trade while account is suspended") 112 ) 113 114 // WalletNoPeersError should be returned when a wallet has no network peers. 115 type WalletNoPeersError struct { 116 AssetID uint32 117 } 118 119 func (e *WalletNoPeersError) Error() string { 120 return fmt.Sprintf("%s wallet has no network peers (check your network or firewall)", unbip(e.AssetID)) 121 } 122 123 // WalletSyncError should be returned when a wallet is still syncing. 124 type WalletSyncError struct { 125 AssetID uint32 126 Progress float32 127 } 128 129 func (e *WalletSyncError) Error() string { 130 return fmt.Sprintf("%s still syncing. progress = %.2f%%", unbip(e.AssetID), e.Progress*100) 131 }