Skip to content

Commit

Permalink
🚀 Improve error handling for net error(s) (#2421)
Browse files Browse the repository at this point in the history
* improve error handling for net error(s)
fixes: reverse proxy support #2419

* Update app.go

Co-authored-by: leonklingele <[email protected]>

* improve error handling for net error(s)
fixes: reverse proxy support #2419

* improve error handling for net error(s)
fixes: reverse proxy support #2419

* improve error handling for net error(s)
fixes: reverse proxy support #2419

---------

Co-authored-by: leonklingele <[email protected]>
  • Loading branch information
ReneWerner87 and leonklingele authored Apr 21, 2023
1 parent c4d2876 commit 9feaf22
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1052,13 +1052,18 @@ func (app *App) serverErrorHandler(fctx *fasthttp.RequestCtx, err error) {
c := app.AcquireCtx(fctx)
defer app.ReleaseCtx(c)

var errNetOP *net.OpError
var (
errNetOP *net.OpError
netErr net.Error
)

switch {
case errors.As(err, new(*fasthttp.ErrSmallBuffer)):
err = ErrRequestHeaderFieldsTooLarge
case errors.As(err, &errNetOP) && errNetOP.Timeout():
err = ErrRequestTimeout
case errors.As(err, &netErr):
err = ErrBadGateway
case errors.Is(err, fasthttp.ErrBodyTooLarge):
err = ErrRequestEntityTooLarge
case errors.Is(err, fasthttp.ErrGetOnly):
Expand Down
14 changes: 14 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,20 @@ func Test_App_serverErrorHandler_Internal_Error(t *testing.T) {
utils.AssertEqual(t, c.fasthttp.Response.StatusCode(), StatusBadRequest)
}

func Test_App_serverErrorHandler_Network_Error(t *testing.T) {
t.Parallel()
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
app.serverErrorHandler(c.fasthttp, &net.DNSError{
Err: "test error",
Name: "test host",
IsTimeout: false,
})
utils.AssertEqual(t, string(c.fasthttp.Response.Body()), utils.StatusMessage(StatusBadGateway))
utils.AssertEqual(t, c.fasthttp.Response.StatusCode(), StatusBadGateway)
}

func Test_App_Nested_Params(t *testing.T) {
t.Parallel()
app := New()
Expand Down

1 comment on commit 9feaf22

@ReneWerner87
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 9feaf22 Previous: c4d2876 Ratio
Benchmark_Ctx_SendString_B 24.19 ns/op 0 B/op 0 allocs/op 9.932 ns/op 0 B/op 0 allocs/op 2.44

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.