Skip to content

Commit

Permalink
Silence broken pipe error
Browse files Browse the repository at this point in the history
Firefox seems to close TCP connections prematurely after accepting some
of the data for the response.
  • Loading branch information
ecnepsnai committed Mar 28, 2024
1 parent b1becee commit c0ad305
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"strconv"
"strings"
"time"

"github.com/ecnepsnai/web/router"
Expand Down Expand Up @@ -153,6 +154,10 @@ func (a API) apiPostHandle(endpointHandle APIHandle, userData interface{}, optio
})
}
if err := json.NewEncoder(w).Encode(response); err != nil {
if strings.Contains(err.Error(), "write: broken pipe") {
return
}

log.PError("Error writing response", map[string]interface{}{
"method": r.HTTP.Method,
"url": r.HTTP.URL,
Expand Down
5 changes: 5 additions & 0 deletions http_easy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"
"strconv"
"strings"
"time"

"github.com/ecnepsnai/web/router"
Expand Down Expand Up @@ -219,6 +220,10 @@ func (h HTTPEasy) httpPostHandle(endpointHandle HTTPEasyHandle, userData interfa

if r.HTTP.Method != "HEAD" && response.Reader != nil {
if copied, err := io.Copy(w, response.Reader); err != nil {
if strings.Contains(err.Error(), "write: broken pipe") {
return
}

log.PError("Error writing response data", map[string]interface{}{
"method": r.HTTP.Method,
"url": r.HTTP.URL,
Expand Down

0 comments on commit c0ad305

Please sign in to comment.