diff options
author | Michael Pratt <mpratt@google.com> | 2019-04-22 18:17:25 -0700 |
---|---|---|
committer | Shentubot <shentubot@google.com> | 2019-04-22 18:20:51 -0700 |
commit | f86c35a51ff92718e36ff6075339300be11e09b3 (patch) | |
tree | a539a750dabc61a19d47740c8b4f770f67977d97 /pkg/state | |
parent | 56927e5317151d5775366c6315f2054e0ae0e23a (diff) |
Clean up state error handling
PiperOrigin-RevId: 244773836
Change-Id: I32223f79d2314fe1ac4ddfc63004fc22ff634adf
Diffstat (limited to 'pkg/state')
-rw-r--r-- | pkg/state/state.go | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/pkg/state/state.go b/pkg/state/state.go index 4b141777e..4486f83a7 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -60,8 +60,8 @@ import ( // ErrState is returned when an error is encountered during encode/decode. type ErrState struct { - // Err is the underlying error. - Err error + // err is the underlying error. + err error // path is the visit path from root to the current object. path string @@ -72,7 +72,17 @@ type ErrState struct { // Error returns a sensible description of the state error. func (e *ErrState) Error() string { - return fmt.Sprintf("%v:\nstate path: %s\n%s", e.Err, e.path, e.trace) + return fmt.Sprintf("%v:\nstate path: %s\n%s", e.err, e.path, e.trace) +} + +// UnwrapErrState returns the underlying error in ErrState. +// +// If err is not *ErrState, err is returned directly. +func UnwrapErrState(err error) error { + if e, ok := err.(*ErrState); ok { + return e.err + } + return err } // Save saves the given object state. @@ -318,9 +328,9 @@ func (sr *recoverable) safely(fn func()) (err error) { if r := recover(); r != nil { es := new(ErrState) if e, ok := r.(error); ok { - es.Err = e + es.err = e } else { - es.Err = fmt.Errorf("%v", r) + es.err = fmt.Errorf("%v", r) } es.path = sr.path() |