github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/nsadapter/httputil/response.go (about) 1 package httputil 2 3 import ( 4 "context" 5 "encoding/json" 6 "net/http" 7 8 "github.com/kyma-incubator/compass/components/director/pkg/httputils" 9 "github.com/kyma-incubator/compass/components/director/pkg/log" 10 ) 11 12 // RespondWithError sends error response with the provided status code and error message 13 func RespondWithError(ctx context.Context, w http.ResponseWriter, status int, err error) { 14 log.C(ctx).WithError(err).Errorf("Responding with error: %v", err) 15 w.Header().Add(httputils.HeaderContentTypeKey, httputils.ContentTypeApplicationJSON) 16 w.WriteHeader(status) 17 errorResponse := ErrorResponse{Error: err} 18 encodingErr := json.NewEncoder(w).Encode(errorResponse) 19 if encodingErr != nil { 20 log.C(ctx).WithError(err).Errorf("Failed to encode error response: %v", err) 21 } 22 }