|
| 1 | +package tcs |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | +) |
| 6 | + |
| 7 | +// DecodingError represents an error that occurs during decoding operations. |
| 8 | +type DecodingError struct { |
| 9 | + ObjectType string |
| 10 | + Text string |
| 11 | + Err error |
| 12 | +} |
| 13 | + |
| 14 | +// Error returns the error message. |
| 15 | +func (e DecodingError) Error() string { |
| 16 | + suffix := e.ObjectType |
| 17 | + if e.Text != "" { |
| 18 | + suffix = fmt.Sprintf("%s, %s", suffix, e.Text) |
| 19 | + } |
| 20 | + |
| 21 | + return fmt.Sprintf("failed to decode %s: %s", suffix, e.Err) |
| 22 | +} |
| 23 | + |
| 24 | +func (e DecodingError) Unwrap() error { |
| 25 | + return e.Err |
| 26 | +} |
| 27 | + |
| 28 | +// NewTxnOpResponseDecodingError returns a new txnOpResponse decoding error. |
| 29 | +func NewTxnOpResponseDecodingError(err error) error { |
| 30 | + if err == nil { |
| 31 | + return nil |
| 32 | + } |
| 33 | + |
| 34 | + return DecodingError{ |
| 35 | + ObjectType: "txnOpResponse", |
| 36 | + Text: "", |
| 37 | + Err: err, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// EncodingError represents an error that occurs during encoding operations. |
| 42 | +type EncodingError struct { |
| 43 | + ObjectType string |
| 44 | + Text string |
| 45 | + Err error |
| 46 | +} |
| 47 | + |
| 48 | +// Error returns the error message. |
| 49 | +func (e EncodingError) Error() string { |
| 50 | + if e.Text == "" { |
| 51 | + return fmt.Sprintf("failed to encode %s: %s", e.ObjectType, e.Err) |
| 52 | + } |
| 53 | + |
| 54 | + return fmt.Sprintf("failed to encode %s, %s: %s", e.ObjectType, e.Text, e.Err) |
| 55 | +} |
| 56 | + |
| 57 | +func (e EncodingError) Unwrap() error { |
| 58 | + return e.Err |
| 59 | +} |
| 60 | + |
| 61 | +// NewOperationEncodingError returns a new operation encoding error. |
| 62 | +func NewOperationEncodingError(text string, err error) error { |
| 63 | + if err == nil { |
| 64 | + return nil |
| 65 | + } |
| 66 | + |
| 67 | + return EncodingError{ |
| 68 | + ObjectType: "operation", |
| 69 | + Text: text, |
| 70 | + Err: err, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// NewPredicateEncodingError returns a new predicate encoding error. |
| 75 | +func NewPredicateEncodingError(text string, err error) error { |
| 76 | + if err == nil { |
| 77 | + return nil |
| 78 | + } |
| 79 | + |
| 80 | + return EncodingError{ |
| 81 | + ObjectType: "predicate", |
| 82 | + Text: text, |
| 83 | + Err: err, |
| 84 | + } |
| 85 | +} |
0 commit comments