Skip to content

Commit

Permalink
requestbody: Type-based error handling for MaxBytesError (#6701)
Browse files Browse the repository at this point in the history
* fix: handle "request body too large" error using type assertion

* fix: address overlooked nil check for MaxBytesError

* fix: replace type assertion with errors.As() for MaxBytesError
  • Loading branch information
rishitashaw authored Nov 22, 2024
1 parent eddbccd commit 8c3dd3d
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion modules/caddyhttp/requestbody/requestbody.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package requestbody

import (
"errors"
"io"
"net/http"
"time"
Expand Down Expand Up @@ -94,7 +95,8 @@ type errorWrapper struct {

func (ew errorWrapper) Read(p []byte) (n int, err error) {
n, err = ew.ReadCloser.Read(p)
if err != nil && err.Error() == "http: request body too large" {
var mbe *http.MaxBytesError
if errors.As(err, &mbe) {
err = caddyhttp.Error(http.StatusRequestEntityTooLarge, err)
}
return
Expand Down

0 comments on commit 8c3dd3d

Please sign in to comment.