Skip to content

Instantly share code, notes, and snippets.

@RussellLuo
Created May 28, 2020 08:08
Show Gist options
  • Save RussellLuo/b903150456d191a7937195b65db6d150 to your computer and use it in GitHub Desktop.
Save RussellLuo/b903150456d191a7937195b65db6d150 to your computer and use it in GitHub Desktop.
Enhanced error handling based on new features introduced in Go 1.13
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