3
0

Fix peer-closed connection error

This commit is contained in:
Peter Grlica
2022-05-04 12:36:08 +02:00
committed by Denis Arh
parent c12385ebaa
commit 495fac3845
2 changed files with 37 additions and 0 deletions
+12
View File
@@ -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())
+25
View File
@@ -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