diff --git a/pkg/errors/http.go b/pkg/errors/http.go index eca5f5f50..6ec89f866 100644 --- a/pkg/errors/http.go +++ b/pkg/errors/http.go @@ -3,11 +3,13 @@ package errors import ( "context" "encoding/json" + "errors" "fmt" "io" "net/http" "sort" "strings" + "syscall" "github.com/cortezaproject/corteza-server/pkg/locale" ) @@ -76,6 +78,11 @@ func writeHttpPlain(w io.Writer, err error, mask bool) { } ) + // handle client-aborted connections with no output + if errors.Is(err, syscall.EPIPE) || errors.Is(err, syscall.ECONNRESET) { + return + } + write("Error: ") write(err.Error()) write("\n") @@ -127,6 +134,11 @@ func writeHttpJSON(ctx context.Context, w io.Writer, err error, mask bool) { }{} ) + // handle client-aborted connections with no output + if errors.Is(err, syscall.EPIPE) || errors.Is(err, syscall.ECONNRESET) { + return + } + if se, is := err.(interface{ Safe() bool }); !is || !se.Safe() || mask { // trim error details when not debugging or error is not safe or maske err = fmt.Errorf(err.Error()) diff --git a/pkg/errors/http_test.go b/pkg/errors/http_test.go index 09ce12185..ced4e1797 100644 --- a/pkg/errors/http_test.go +++ b/pkg/errors/http_test.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "os" + "syscall" "testing" "github.com/stretchr/testify/require" @@ -24,6 +25,30 @@ func Example_writeHttpJSON() { // {"error":{"message":"dummy error"}} } +func Example_writeHttpJSON_clientAbortedConnectionReset() { + writeHttpJSON(context.Background(), os.Stdout, syscall.ECONNRESET, true) + + // Output: +} + +func Example_writeHttpPlain_clientAbortedConnectionReset() { + writeHttpPlain(os.Stdout, syscall.ECONNRESET, true) + + // Output: +} + +func Example_writeHttpJSON_clientAbortedConnectionPipe() { + writeHttpJSON(context.Background(), os.Stdout, syscall.EPIPE, true) + + // Output: +} + +func Example_writeHttpPlain_clientAbortedConnectionPipe() { + writeHttpPlain(os.Stdout, syscall.EPIPE, true) + + // Output: +} + func Example_writeHttpPlain_masked() { err := New(0, "dummy error", Meta("a", "b"), Meta(&Error{}, "nope")) err.stack = nil // will not test the stack as file path & line numbers might change