decred.org/dcrdex@v1.0.5/server/db/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 db 5 6 import "errors" 7 8 // TODO: Consider changing error types to Error and DetailedError for direct 9 // errors package unwrapping and tests with Is/As: 10 11 // Error is just a basic error. 12 //type Error string 13 14 // Error satisfies the error interface. 15 // func (e Error) Error() string { 16 // return string(e) 17 // } 18 19 // DetailedError pairs an Error with details. 20 // type DetailedError struct { 21 // wrapped Error 22 // detail string 23 // } 24 25 // Error satisfies the error interface, combining the wrapped error message with 26 // the details. 27 // func (e DetailedError) Error() string { 28 // return e.wrapped.Error() + ": " + e.detail 29 // } 30 31 // NewDetailedError wraps the provided Error with details in a DetailedError, 32 // facilitating the use of errors.Is and errors.As via errors.Unwrap. 33 // func NewDetailedError(err Error, detail string) DetailedError { 34 // return DetailedError{ 35 // wrapped: err, 36 // detail: detail, 37 // } 38 // } 39 40 // ArchiveError is the error type used by archivist for certain recognized 41 // errors. Not all returned errors will be of this type. 42 type ArchiveError struct { 43 Code uint16 44 Detail string 45 } 46 47 // The possible Code values in an ArchiveError. 48 const ( 49 ErrGeneralFailure uint16 = iota 50 ErrUnknownMatch 51 ErrUnknownOrder 52 ErrUnsupportedMarket 53 ErrInvalidOrder 54 ErrReusedCommit 55 ErrOrderNotExecuted 56 ErrUpdateCount 57 ErrAccountUnknown 58 ErrAccountBadFeeInfo 59 ErrUnknownFeeKey 60 ) 61 62 func (ae ArchiveError) Error() string { 63 desc := "unrecognized error" 64 switch ae.Code { 65 case ErrGeneralFailure: 66 desc = "general failure" 67 case ErrUnknownMatch: 68 desc = "unknown match" 69 case ErrUnknownOrder: 70 desc = "unknown order" 71 case ErrUnsupportedMarket: 72 desc = "unsupported market" 73 case ErrInvalidOrder: 74 desc = "invalid order" 75 case ErrReusedCommit: 76 desc = "order commit reused" 77 case ErrOrderNotExecuted: 78 desc = "order not in executed status" 79 case ErrUpdateCount: 80 desc = "unexpected number of rows updated" 81 case ErrAccountUnknown: 82 desc = "account unknown" 83 case ErrAccountBadFeeInfo: 84 desc = "mismatching fee address or asset" 85 case ErrUnknownFeeKey: 86 desc = "unknown fee key" 87 } 88 89 if ae.Detail == "" { 90 return desc 91 } 92 return desc + ": " + ae.Detail 93 } 94 95 // SameErrorTypes checks for error equality or ArchiveError.Code equality if 96 // both errors are of type ArchiveError. 97 func SameErrorTypes(errA, errB error) bool { 98 var arA, arB ArchiveError 99 if errors.As(errA, &arA) && errors.As(errB, &arB) { 100 return arA.Code == arB.Code 101 } 102 103 return errors.Is(errA, errB) 104 } 105 106 // IsErrGeneralFailure returns true if the error is of type ArchiveError and has 107 // code ErrGeneralFailure. 108 func IsErrGeneralFailure(err error) bool { 109 var errA ArchiveError 110 return errors.As(err, &errA) && errA.Code == ErrGeneralFailure 111 } 112 113 // IsErrOrderUnknown returns true if the error is of type ArchiveError and has 114 // code ErrUnknownOrder. 115 func IsErrOrderUnknown(err error) bool { 116 var errA ArchiveError 117 return errors.As(err, &errA) && errA.Code == ErrUnknownOrder 118 } 119 120 // IsErrMatchUnknown returns true if the error is of type ArchiveError and has 121 // code ErrUnknownMatch. 122 func IsErrMatchUnknown(err error) bool { 123 var errA ArchiveError 124 return errors.As(err, &errA) && errA.Code == ErrUnknownMatch 125 } 126 127 // IsErrMatchUnsupported returns true if the error is of type ArchiveError and 128 // has code ErrUnsupportedMarket. 129 func IsErrMatchUnsupported(err error) bool { 130 var errA ArchiveError 131 return errors.As(err, &errA) && errA.Code == ErrUnsupportedMarket 132 } 133 134 // IsErrInvalidOrder returns true if the error is of type ArchiveError and has 135 // code ErrInvalidOrder. 136 func IsErrInvalidOrder(err error) bool { 137 var errA ArchiveError 138 return errors.As(err, &errA) && errA.Code == ErrInvalidOrder 139 } 140 141 // IsErrReusedCommit returns true if the error is of type ArchiveError and has 142 // code ErrReusedCommit. 143 func IsErrReusedCommit(err error) bool { 144 var errA ArchiveError 145 return errors.As(err, &errA) && errA.Code == ErrReusedCommit 146 } 147 148 // IsErrOrderNotExecuted returns true if the error is of type ArchiveError and 149 // has code ErrOrderNotExecuted. 150 func IsErrOrderNotExecuted(err error) bool { 151 var errA ArchiveError 152 return errors.As(err, &errA) && errA.Code == ErrOrderNotExecuted 153 } 154 155 // IsErrUpdateCount returns true if the error is of type ArchiveError and has 156 // code ErrUpdateCount. 157 func IsErrUpdateCount(err error) bool { 158 var errA ArchiveError 159 return errors.As(err, &errA) && errA.Code == ErrUpdateCount 160 } 161 162 // IsErrAccountUnknown returns true if the error is of type ArchiveError and has 163 // code ErrAccountUnknown. 164 func IsErrAccountUnknown(err error) bool { 165 var errA ArchiveError 166 return errors.As(err, &errA) && errA.Code == ErrAccountUnknown 167 } 168 169 // IsErrUnknownFeeKey returns true if the error is of type ArchiveError and has 170 // code ErrUnknownFeeKey. 171 func IsErrUnknownFeeKey(err error) bool { 172 var errA ArchiveError 173 return errors.As(err, &errA) && errA.Code == ErrUnknownFeeKey 174 }