-
-
Notifications
You must be signed in to change notification settings - Fork 564
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
Use errors.As() #3542
Use errors.As() #3542
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you this is great! There is one case where we may not need to use As
.
@@ -134,7 +135,11 @@ func (c *DSLContext) Record(err *Error) { | |||
if c.Errors == nil { | |||
c.Errors = MultiError{err} | |||
} else { | |||
c.Errors = append(c.Errors.(MultiError), err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if in this case this is necessary? The type of the Errors
field is controlled by Goa and guaranteed to be MultiError
, see https://github.com/goadesign/goa/blob/v3/eval/context.go#L16
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but it can be a different error type in the Go type system. For clarity, how about changing DSLContext.Errors
to MultiError
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes making that change makes sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eval/eval.go
Outdated
@@ -71,14 +72,20 @@ func Execute(fn func(), def Expression) bool { | |||
} | |||
var startCount int | |||
if Context.Errors != nil { | |||
startCount = len(Context.Errors.(MultiError)) | |||
var e MultiError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, not sure this is needed.
} | ||
Context.Stack = append(Context.Stack, def) | ||
fn() | ||
Context.Stack = Context.Stack[:len(Context.Stack)-1] | ||
var endCount int | ||
if Context.Errors != nil { | ||
endCount = len(Context.Errors.(MultiError)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also probably not needed
e, ok := err.(*ServiceError) | ||
if !ok { | ||
var e *ServiceError | ||
if !errors.As(err, &e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this use Is
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be As
. The Go Blog says
https://go.dev/blog/go1.13-errorse
In the simplest case, the errors.Is function behaves like a comparison to a sentinel error, and the errors.As function behaves like a type assertion
I added test for asError()
. It fails if you replace As
with Is
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, thanks for the clarification!
Thank you! |
No description provided.