go: net/http: add MaxBytesError
What version of Go are you using (go version)?
$ go version go version go1.12 darwin/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env)?
go env Output
$ go env GOARCH="amd64" GOBIN="/Users/foo/go/bin" GOCACHE="/Users/foo/Library/Caches/go-build" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOOS="darwin" GOPATH="/Users/foo/go" GOPROXY="" GORACE="" GOROOT="/usr/local/Cellar/go/1.12/libexec" GOTMPDIR="" GOTOOLDIR="/usr/local/Cellar/go/1.12/libexec/pkg/tool/darwin_amd64" GCCGO="gccgo" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/lf/rbfblwvx6rx3xhm68yksmqjwdv1dsf/T/go-build261266046=/tmp/go-build -gno-record-gcc-switches -fno-common"
What 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 by MaxBytesReader 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
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 26
- Comments: 18 (16 by maintainers)
Commits related to this issue
- net/http: add MaxBytesError Fixes #30715 — committed to carlmjohnson/go by carlmjohnson 3 years ago
- net/http: add MaxBytesError Fixes #30715 — committed to carlmjohnson/go by carlmjohnson 3 years ago
No change in consensus, so accepted. π This issue now tracks the work of implementing the proposal. β rsc for the proposal review group
We probably donβt need the special definition at the end. You can use
if errors.As(err, new(*http.MaxBytesError)) { β¦ }
This proposal has been added to the active column of the proposals project and will now be reviewed at the weekly proposal review meetings. β rsc for the proposal review group
Based on the discussion above, this proposal seems like a likely accept. β rsc for the proposal review group
FWIW, #41493 is a dupe with all positive feedback. I propose that MaxByteReader should return errors of the type
ErrorMaxBytesExceeded struct { MaxSize int64 }that can be checked witherrors.As.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.