|
| 1 | +package access |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/onflow/flow-go/module/irrecoverable" |
| 9 | +) |
| 10 | + |
| 11 | +// RequireNoError returns nil if error is nil, otherwise throws an irrecoverable exception |
| 12 | +func RequireNoError(ctx context.Context, err error) error { |
| 13 | + if err == nil { |
| 14 | + return nil |
| 15 | + } |
| 16 | + |
| 17 | + irrecoverable.Throw(ctx, err) |
| 18 | + return irrecoverable.NewException(err) |
| 19 | +} |
| 20 | + |
| 21 | +// RequireErrorIs returns the error if it unwraps to any of the provided target error types |
| 22 | +// Otherwise, it throws an irrecoverable exception |
| 23 | +func RequireErrorIs(ctx context.Context, err error, targetErrs ...error) error { |
| 24 | + if err == nil { |
| 25 | + return nil |
| 26 | + } |
| 27 | + |
| 28 | + for _, targetErr := range targetErrs { |
| 29 | + if errors.Is(err, targetErr) { |
| 30 | + return err |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + irrecoverable.Throw(ctx, err) |
| 35 | + return irrecoverable.NewException(err) |
| 36 | +} |
| 37 | + |
| 38 | +// InvalidRequest indicates that the client's request was malformed or invalid |
| 39 | +type InvalidRequest struct { |
| 40 | + err error |
| 41 | +} |
| 42 | + |
| 43 | +func NewInvalidRequest(err error) InvalidRequest { |
| 44 | + return InvalidRequest{err: err} |
| 45 | +} |
| 46 | + |
| 47 | +func (e InvalidRequest) Error() string { |
| 48 | + return fmt.Sprintf("invalid argument: %v", e.err) |
| 49 | +} |
| 50 | + |
| 51 | +func (e InvalidRequest) Unwrap() error { |
| 52 | + return e.err |
| 53 | +} |
| 54 | + |
| 55 | +func IsInvalidRequest(err error) bool { |
| 56 | + var errInvalidRequest InvalidRequest |
| 57 | + return errors.As(err, &errInvalidRequest) |
| 58 | +} |
| 59 | + |
| 60 | +// DataNotFound indicates that the requested data was not found on the system |
| 61 | +type DataNotFound struct { |
| 62 | + dataType string |
| 63 | + err error |
| 64 | +} |
| 65 | + |
| 66 | +func NewDataNotFound(dataType string, err error) DataNotFound { |
| 67 | + return DataNotFound{dataType: dataType, err: err} |
| 68 | +} |
| 69 | + |
| 70 | +func (e DataNotFound) Error() string { |
| 71 | + return fmt.Sprintf("data not found for %s: %v", e.dataType, e.err) |
| 72 | +} |
| 73 | + |
| 74 | +func (e DataNotFound) Unwrap() error { |
| 75 | + return e.err |
| 76 | +} |
| 77 | + |
| 78 | +func IsDataNotFound(err error) bool { |
| 79 | + var errDataNotFound DataNotFound |
| 80 | + return errors.As(err, &errDataNotFound) |
| 81 | +} |
| 82 | + |
| 83 | +// InternalError indicates that a non-fatal internal error occurred |
| 84 | +// IMPORTANT: this should only be used for benign internal errors. Fatal or irrecoverable system |
| 85 | +// errors must be handled explicitly. |
| 86 | +type InternalError struct { |
| 87 | + err error |
| 88 | +} |
| 89 | + |
| 90 | +func NewInternalError(err error) InternalError { |
| 91 | + return InternalError{err: err} |
| 92 | +} |
| 93 | + |
| 94 | +func (e InternalError) Error() string { |
| 95 | + return fmt.Sprintf("internal error: %v", e.err) |
| 96 | +} |
| 97 | + |
| 98 | +func (e InternalError) Unwrap() error { |
| 99 | + return e.err |
| 100 | +} |
| 101 | + |
| 102 | +func IsInternalError(err error) bool { |
| 103 | + var errInternalError InternalError |
| 104 | + return errors.As(err, &errInternalError) |
| 105 | +} |
| 106 | + |
| 107 | +// OutOfRangeError indicates that the request was for data that is outside of the available range. |
| 108 | +// This is a more specific version of DataNotFound, where the data is known to eventually exist, but |
| 109 | +// currently is not known. |
| 110 | +// For example, querying data for a height above the current finalized height. |
| 111 | +type OutOfRangeError struct { |
| 112 | + err error |
| 113 | +} |
| 114 | + |
| 115 | +func NewOutOfRangeError(err error) OutOfRangeError { |
| 116 | + return OutOfRangeError{err: err} |
| 117 | +} |
| 118 | + |
| 119 | +func (e OutOfRangeError) Error() string { |
| 120 | + return fmt.Sprintf("out of range: %v", e.err) |
| 121 | +} |
| 122 | + |
| 123 | +func (e OutOfRangeError) Unwrap() error { |
| 124 | + return e.err |
| 125 | +} |
| 126 | + |
| 127 | +func IsOutOfRangeError(err error) bool { |
| 128 | + var errOutOfRangeError OutOfRangeError |
| 129 | + return errors.As(err, &errOutOfRangeError) |
| 130 | +} |
| 131 | + |
| 132 | +// FailedPrecondition indicates that a precondition for the operation was not met |
| 133 | +// This is a more specific version of InvalidRequest, where the request is valid, but the system |
| 134 | +// is not currently in a state to fulfill the request (but may be in the future). |
| 135 | +type FailedPrecondition struct { |
| 136 | + err error |
| 137 | +} |
| 138 | + |
| 139 | +func NewFailedPrecondition(err error) FailedPrecondition { |
| 140 | + return FailedPrecondition{err: err} |
| 141 | +} |
| 142 | + |
| 143 | +func (e FailedPrecondition) Error() string { |
| 144 | + return fmt.Sprintf("precondition failed: %v", e.err) |
| 145 | +} |
| 146 | + |
| 147 | +func (e FailedPrecondition) Unwrap() error { |
| 148 | + return e.err |
| 149 | +} |
| 150 | + |
| 151 | +func IsPreconditionFailed(err error) bool { |
| 152 | + var errPreconditionFailed FailedPrecondition |
| 153 | + return errors.As(err, &errPreconditionFailed) |
| 154 | +} |
0 commit comments