-
Notifications
You must be signed in to change notification settings - Fork 114
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
Wrapping original issue in rest.Error #269
Comments
We could maybe change the conversion of errors to Then you can implement your own error types as needed, and implement the following method to asign itself to a func (err MyError) As(target interface{}) bool {
v, ok := target.(*http.Error)
if !ok { return false }
// set v. values
return true
} We could for conveniences provide something like this in the type codeError struct{
code int
err error
}
func StatusError(code int, err error) error {
return codeError{code: code, err: err}
}
func (err codeError) Unwrap() error {
return err.err
}
func (err codeError) Error() string {
if err.err == nil {
return http.StatusText(err.code)
}
return err.Error()
}
func (err codeError) As(target interface{}) bool {
v, ok := target.(*http.Error)
if !ok { return false }
v.Code = err.Code()
v.Message = err.Error()
return true If we do the conversion to |
There are other ways to do this, this is just one method. |
Thank you @smyrman for the detailed response. Let me think about it a little. |
Likewise we can match against errors from To wrap an error in the most plain way possible, you can use e.g. Should change relevant places where errors are returned to return non-pointer errors. |
Actually I am using another error package for hooks: |
Hi @smyrman , sorry for the big delay. Regarding this issue, can you take a look at this patch: It seems to work, without introducing new types. We can change the |
Which issue does this patch solve? It seams to return exactly the same as before, excpet that the NewError function returns two values, where one is now a generic |
There is an
rest.Error
type that captures all unrecognized errors from Hooks:However sometimes, enriched errors are used(preserving call-trace), that become useless once stringed through
err.Error()
. Can we preseve original error like this:So I can use the errors in
func (r ResponseFormatter) FormatError(
and send them to say Sentry or logs.This might be a breaking change if someone is not following
go vet
recommendations(like inrest-layer
source code :)The text was updated successfully, but these errors were encountered: