-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib: Rethink common.GetErrorMsg approach #3042
Comments
E.g. the cause of #3037 |
With Go 1.13 moving towards the current type SimpleError string
func (e SimpleError) Error() string {
return string(e)
}
type BasicError struct {
// Error message
Msg SimpleError
// Error context, for logging purposes only
logCtx []interface{}
// Nested error, if any.
Err error
}
func (be BasicError) Is(err error) bool {
switch other := err.(type) {
case BasicError:
return be.Msg == other.Msg
case SimpleError:
return be.Msg == other
default:
return false
}
}
func (be BasicError) Unrwap() error {
return be.GetErr()
} Note that Note that using old string messages would still be allowed (except const (
ErrInvariantViolation common.SimpleError = "TRC invariant violation"
)
func foo() error {
return common.NewBasicError(ErrInvariantViolation, nil, "ctx", 1)
}
func main() {
err := foo()
if xerrors.Is(err, ErrInvariantViolation) {
fmt.Sprintf("Invariant violation\n")
}
} Note that with this we can define all errors as constants (no vars for simple text only errors as would be the case with |
This commit adds Unwrap and Is methods to the BasicError type. It also adds a SimpleError type that can be used for error string constants. This will allow us to get rid of brittle error checks with GetErrorMsg=="some string". Contributes scionproto#3042
This commit adds Unwrap and Is methods to the BasicError type. It also adds a SimpleError type that can be used for error string constants. This will allow us to get rid of brittle error checks with GetErrorMsg=="some string". Contributes scionproto#3042
This commit adds Unwrap and Is methods to the BasicError type. It also adds a SimpleError type that can be used for error string constants. This will allow us to get rid of brittle error checks with GetErrorMsg=="some string". Contributes #3042
This should in the long term replace: -errors.New -common.NewBasicError The package is ready to be used with Go1.13 (or xerrors) Is and As. Also it provides all the functionality that was provided by common.BasicError/common.MultiError. By putting it in a serrors package we can make the API slightly nicer to use. Contributes #3042
Those checks are dangerous since a refactor could easily break them. Using xerrors.Is instead is safer against refactors. Contributes scionproto#3042
Those checks are dangerous since a refactor could easily break them. Using xerrors.Is instead is safer against refactors. Contributes #3042
serrors support is in (including a linter) |
The current
common.GetErrorMsg
approach is very brittle. If during a refactor the error is wrapped. or a non-constant string is modified, the check will fail.We should consider an approach similar to
xerrors.Is
or just usexerrors.Is
and letcommon.BasicError
implement anIs
method.The text was updated successfully, but these errors were encountered: