Skip to content

Commit

Permalink
Log body and status code in WithErrorResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
ggreer committed Nov 8, 2024
1 parent 776c4f4 commit ad10d20
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions pkg/uhttp/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const (
cacheTTLDefault = 3600 // 3600 seconds = one hour
)

const maxBodySize = 4096

type WrapperResponse struct {
Header http.Header
Body []byte
Expand Down Expand Up @@ -185,7 +187,7 @@ func WithJSONResponse(response interface{}) DoOption {
if !IsJSONContentType(contentHeader) {
if len(resp.Body) != 0 {
// we want to see the body regardless
return fmt.Errorf("unexpected content type for JSON response: %s. status code: %d. body: «%s»", contentHeader, resp.StatusCode, logBody(resp.Body, 4096))
return fmt.Errorf("unexpected content type for JSON response: %s. status code: %d. body: «%s»", contentHeader, resp.StatusCode, logBody(resp.Body, maxBodySize))
}
return fmt.Errorf("unexpected content type for JSON response: %s. status code: %d", contentHeader, resp.StatusCode)
}
Expand All @@ -194,7 +196,7 @@ func WithJSONResponse(response interface{}) DoOption {
}
err := json.Unmarshal(resp.Body, response)
if err != nil {
return fmt.Errorf("failed to unmarshal json response: %w. status code: %d. body %v", err, resp.StatusCode, logBody(resp.Body, 4096))
return fmt.Errorf("failed to unmarshal json response: %w. status code: %d. body %v", err, resp.StatusCode, logBody(resp.Body, maxBodySize))
}
return nil
}
Expand All @@ -217,13 +219,15 @@ func WithErrorResponse(resource ErrorResponse) DoOption {
return nil
}

if !IsJSONContentType(resp.Header.Get(ContentType)) {
return fmt.Errorf("%v", string(resp.Body))
contentHeader := resp.Header.Get(ContentType)

if !IsJSONContentType(contentHeader) {
return fmt.Errorf("unexpected content type for JSON error response: %s. status code: %d. body: «%s»", contentHeader, resp.StatusCode, logBody(resp.Body, maxBodySize))
}

// Decode the JSON response body into the ErrorResponse
if err := json.Unmarshal(resp.Body, &resource); err != nil {
return status.Error(codes.Unknown, "Request failed with unknown error")
return fmt.Errorf("failed to unmarshal JSON error response: %w. status code: %d. body %v", err, resp.StatusCode, logBody(resp.Body, maxBodySize))
}

// Construct a more detailed error message
Expand Down

0 comments on commit ad10d20

Please sign in to comment.