decred.org/dcrdex@v1.0.5/client/asset/zec/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 zec 5 6 import ( 7 "errors" 8 "fmt" 9 ) 10 11 // Error codes here are used on the frontend. 12 const ( 13 errNoFundsRequested = iota 14 errBalanceRetrieval 15 errInsufficientBalance 16 errUTxORetrieval 17 errLockUnspent 18 errFunding 19 errShieldedFunding 20 errNoTx 21 errGetChainInfo 22 errGetNetInfo 23 errBadInput 24 errMaxLock 25 errNoteCounts 26 ) 27 28 // Error is an error code and a wrapped error. 29 type Error struct { 30 code int 31 err error 32 } 33 34 // Error returns the error string. Satisfies the error interface. 35 func (e *Error) Error() string { 36 return e.err.Error() 37 } 38 39 // Code returns the error code. 40 func (e *Error) Code() *int { 41 return &e.code 42 } 43 44 // Unwrap returns the underlying wrapped error. 45 func (e *Error) Unwrap() error { 46 return e.err 47 } 48 49 // newError is a constructor for a new Error. 50 func newError(code int, s string, a ...any) error { 51 return &Error{ 52 code: code, 53 err: fmt.Errorf(s, a...), // s may contain a %w verb to wrap an error 54 } 55 } 56 57 // codedError converts the error to an Error with the specified code. 58 func codedError(code int, err error) error { 59 return &Error{ 60 code: code, 61 err: err, 62 } 63 } 64 65 // errorHasCode checks whether the error is an Error and has the specified code. 66 func errorHasCode(err error, code int) bool { 67 var e *Error 68 return errors.As(err, &e) && e.code == code 69 } 70 71 // UnwrapErr returns the result of calling the Unwrap method on err, 72 // until it returns a non-wrapped error. 73 func UnwrapErr(err error) error { 74 InnerErr := errors.Unwrap(err) 75 if InnerErr == nil { 76 return err 77 } 78 return UnwrapErr(InnerErr) 79 }