-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
requestbody: Type-based error handling for MaxBytesError
#6701
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 pretty good, but there's a lint failure. I have a suggestion that I think could work but I just did it here in the textbox, so if you want to verify locally that it compiles then I think we'll be all set. :)
if err != nil { | ||
if _, ok := err.(*http.MaxBytesError); ok { |
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.
if err != nil { | |
if _, ok := err.(*http.MaxBytesError); ok { | |
var mbe *http.MaxBytesError | |
if errors.As(err, &mbe) { |
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'm sorry I didn’t see your comment earlier. I’ve made different changes. Specifically, I removed the redundant nil
check and used the type assertion directly for *HTTP.MaxBytesError
since the type assertion already guarantees that the error isn’t nil when ok is true.
That said, I really like this project and would love to be more involved. Would it be possible to connect on Slack? I’m eager to contribute more!
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.
Thanks. I still think errors.As()
is recommended since the error value can be wrapped, thus failing a type assertion.
And yes! I'm running out the door but when I get back I'll send a Slack invite.
I was thinking we'd use https://pkg.go.dev/errors#example-Is |
MaxBytesError
@francislavoie It's a type, not a value. |
Oh right, they implemented |
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.
LGTM. Thanks! @rishitashaw What is a good email address for me to invite?
Thanks @mholt |
Replaced the hardcoded error string comparison with a type assertion for http.MaxBytesError to make the code more robust and idiomatic.
Before:
After:
Related to #6700