Skip to content

Commit

Permalink
fix: log final writer error instead of handling (#1564)
Browse files Browse the repository at this point in the history
If writing to the ResponseWriter fails, then there's no point in trying
to handle the error recursively and write again. Just log with a
warning.

Should deal with some endless-recursion edge cases.
  • Loading branch information
hf committed Apr 30, 2024
1 parent 374a9c2 commit 170bd66
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions internal/api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func HandleResponseError(err error, w http.ResponseWriter, r *http.Request) {
output.Payload.Reasons = e.Reasons

if jsonErr := sendJSON(w, http.StatusUnprocessableEntity, output); jsonErr != nil && jsonErr != context.DeadlineExceeded {
HandleResponseError(jsonErr, w, r)
log.WithError(jsonErr).Warn("Failed to send JSON on ResponseWriter")
}

} else {
Expand All @@ -225,7 +225,7 @@ func HandleResponseError(err error, w http.ResponseWriter, r *http.Request) {
output.Payload.Reasons = e.Reasons

if jsonErr := sendJSON(w, output.HTTPStatus, output); jsonErr != nil && jsonErr != context.DeadlineExceeded {
HandleResponseError(jsonErr, w, r)
log.WithError(jsonErr).Warn("Failed to send JSON on ResponseWriter")
}
}

Expand Down Expand Up @@ -253,7 +253,7 @@ func HandleResponseError(err error, w http.ResponseWriter, r *http.Request) {
}

if jsonErr := sendJSON(w, e.HTTPStatus, resp); jsonErr != nil && jsonErr != context.DeadlineExceeded {
HandleResponseError(jsonErr, w, r)
log.WithError(jsonErr).Warn("Failed to send JSON on ResponseWriter")
}
} else {
if e.ErrorCode == "" {
Expand All @@ -267,20 +267,20 @@ func HandleResponseError(err error, w http.ResponseWriter, r *http.Request) {
// Provide better error messages for certain user-triggered Postgres errors.
if pgErr := utilities.NewPostgresError(e.InternalError); pgErr != nil {
if jsonErr := sendJSON(w, pgErr.HttpStatusCode, pgErr); jsonErr != nil && jsonErr != context.DeadlineExceeded {
HandleResponseError(jsonErr, w, r)
log.WithError(jsonErr).Warn("Failed to send JSON on ResponseWriter")
}
return
}

if jsonErr := sendJSON(w, e.HTTPStatus, e); jsonErr != nil && jsonErr != context.DeadlineExceeded {
HandleResponseError(jsonErr, w, r)
log.WithError(jsonErr).Warn("Failed to send JSON on ResponseWriter")
}
}

case *OAuthError:
log.WithError(e.Cause()).Info(e.Error())
if jsonErr := sendJSON(w, http.StatusBadRequest, e); jsonErr != nil && jsonErr != context.DeadlineExceeded {
HandleResponseError(jsonErr, w, r)
log.WithError(jsonErr).Warn("Failed to send JSON on ResponseWriter")
}

case ErrorCause:
Expand All @@ -296,7 +296,7 @@ func HandleResponseError(err error, w http.ResponseWriter, r *http.Request) {
}

if jsonErr := sendJSON(w, http.StatusInternalServerError, resp); jsonErr != nil && jsonErr != context.DeadlineExceeded {
HandleResponseError(jsonErr, w, r)
log.WithError(jsonErr).Warn("Failed to send JSON on ResponseWriter")
}
} else {
httpError := HTTPError{
Expand All @@ -306,7 +306,7 @@ func HandleResponseError(err error, w http.ResponseWriter, r *http.Request) {
}

if jsonErr := sendJSON(w, http.StatusInternalServerError, httpError); jsonErr != nil && jsonErr != context.DeadlineExceeded {
HandleResponseError(jsonErr, w, r)
log.WithError(jsonErr).Warn("Failed to send JSON on ResponseWriter")
}
}
}
Expand Down

0 comments on commit 170bd66

Please sign in to comment.