-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
net/http: add MaxBytesError #30715
Comments
There is a similar issue in the |
@rsc At a very least set those string as constants, current code looks ugly and stands against all the things that we love in golang.
|
@rsc, I think this issue fell off the tracker because it's from before the "Proposal" tag. Can this be tagged? If the proposal is accepted, I'm happy to file the CL. |
CC @neild |
FWIW, #41493 is a dupe with all positive feedback. I propose that MaxByteReader should return errors of the type |
A Wrapping the incoming request body in a This proposal, as I understand it, is to return a defined error when a This seems reasonable to me. The
A couple questions, and my proposed answers:
|
Not necessarily. I want to wrap different handlers in different middleware, so that the processImage handler has a 25MB limit, but the processUser handler has a 1MB limit, for example. I want to be able to write generic error response middleware that can prompt the user with different messages for different overages. |
What if instead of defining an error type, we add |
If it is going to be generated by MaxBytesReader, perhaps it should be a MaxBytesError? |
This proposal has been added to the active column of the proposals project |
I would be happy with either of
The struct version is more extensible, but I don't foresee it needing extension, so int64 version is fine too.
|
ping @neild re #30715 (comment) |
I'm not really enthusiastic about increasing
I don't think we should add A struct is better than an We should maybe define that
tl;dr, I'm reluctantly in favor of adding |
We probably don't need the special definition at the end. if errors.As(err, new(*http.MaxBytesError)) { ... } |
Change https://golang.org/cl/361397 mentions this issue: |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 |
Issue title should be changed to "net/http: add MaxBytesError". |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Upon limiting the maximum request size (by using
MaxBytesReader
as in https://stackoverflow.com/a/28292505/381140) I wanted to specially handle too large request body error in a clean way with error type assertion, and return custom response body.More people asked for the same feature: https://groups.google.com/forum/#!topic/golang-nuts/gMzVGDgPyrY
What did you expect to see?
I expected net/http to have
RequestBodyTooLargeError
defined and returned byMaxBytesReader
so type assertion can be done instead of error message comparison.What did you see instead?
Trivial error string implementation is returned https://golang.org/src/net/http/request.go#L1114
The text was updated successfully, but these errors were encountered: