Created
May 28, 2020 08:08
-
-
Save RussellLuo/b903150456d191a7937195b65db6d150 to your computer and use it in GitHub Desktop.
Enhanced error handling based on new features introduced in Go 1.13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package werror | |
import ( | |
"fmt" | |
) | |
type Error struct { | |
Err error // the underlying error | |
Str string | |
} | |
func Wrap(err error) *Error { | |
return &Error{ | |
Err: err, | |
} | |
} | |
func (e *Error) SetError(err error) *Error { | |
e.Str = err.Error() | |
return e | |
} | |
func (e *Error) SetErrorf(format string, a ...interface{}) *Error { | |
e.Str = fmt.Sprintf(format, a...) | |
return e | |
} | |
// Error implements the error interface. | |
func (e *Error) Error() string { return e.Str } | |
// Unwrap follows the Unwrap convention introduced in Go 1.13, | |
// See https://blog.golang.org/go1.13-errors | |
func (e *Error) Unwrap() error { return e.Err } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment