Skip to content

log error for responseWriter.Write() call when writing conjure errors to response writer #706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions conjure-go-contract/errors/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
package errors

import (
"context"
"encoding/json"
"net/http"

"github.com/palantir/conjure-go-runtime/v2/conjure-go-contract/codecs"
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log"
)

// WriteErrorResponse writes error to the response writer.
Expand Down Expand Up @@ -52,3 +54,35 @@ func WriteErrorResponse(w http.ResponseWriter, e Error) {
w.WriteHeader(e.Code().StatusCode())
_, _ = w.Write(marshaledError) // There is nothing we can do on write failure.
}

func WriteErrorResponseWithContext(ctx context.Context, w http.ResponseWriter, e Error) {
var marshaledError []byte
var err error

// First try to marshal with custom handling (if present)
if marshaler, ok := e.(json.Marshaler); ok {
marshaledError, err = codecs.JSON.Marshal(marshaler)
}
// If we fail, use best-effort conversion to SerializableError.
if marshaledError == nil || err != nil {
params, err := codecs.JSON.Marshal(mergeParams(e)) // on failure, params will be nil
if err != nil {
params = nil
}
// This should never fail, since all fields other than params are primitives
// and we fall back to empty params if they fail above. Nothing we can do otherwise.
marshaledError, _ = codecs.JSON.Marshal(SerializableError{
ErrorCode: e.Code(),
ErrorName: e.Name(),
ErrorInstanceID: e.InstanceID(),
Parameters: params,
})
}

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(e.Code().StatusCode())
_, rwWriteErr := w.Write(marshaledError) // There is nothing we can do on write failure.
if rwWriteErr != nil {
svc1log.FromContext(ctx).Error("Failed to write response", svc1log.Stacktrace(rwWriteErr))
}
}
2 changes: 1 addition & 1 deletion conjure-go-server/httpserver/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch e := cause.(type) {
case errors.Error:
// if error is a conjure error, use WriteErrorResponse utility
errors.WriteErrorResponse(w, e)
errors.WriteErrorResponseWithContext(r.Context(), w, e)
case json.Marshaler:
// else if error is a json marshaler, write as json
WriteJSONResponse(w, e, status)
Expand Down