Skip to content

Commit

Permalink
Remove logBody unexported function
Browse files Browse the repository at this point in the history
  • Loading branch information
shackra committed Jan 11, 2025
1 parent 0017a64 commit e9a3deb
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions pkg/uhttp/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ const (
authorizationHeader = "Authorization"
)

const maxBodySize = 4096

type WrapperResponse struct {
Header http.Header
Body []byte
Expand Down Expand Up @@ -141,8 +139,8 @@ 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, maxBodySize))
// to print the response, set the envvar BATON_DEBUG_PRINT_RESPONSE_BODY as non-empty, instead
return fmt.Errorf("unexpected content type for JSON response: %s. status code: %d", contentHeader, resp.StatusCode)
}
return fmt.Errorf("unexpected content type for JSON response: %s. status code: %d", contentHeader, resp.StatusCode)
}
Expand All @@ -151,7 +149,8 @@ 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, maxBodySize))
// to print the response, set the envvar BATON_DEBUG_PRINT_RESPONSE_BODY as non-empty, instead
return fmt.Errorf("failed to unmarshal json response: %w. status code: %d", err, resp.StatusCode)
}
return nil
}
Expand All @@ -165,19 +164,13 @@ func WithAlwaysJSONResponse(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, maxBodySize))
// to print the response, set the envvar BATON_DEBUG_PRINT_RESPONSE_BODY as non-empty, instead
return fmt.Errorf("failed to unmarshal json response: %w. status code: %d", err, resp.StatusCode)
}
return nil
}
}

func logBody(body []byte, size int) string {
if len(body) > size {
return string(body[:size]) + " ..."
}
return string(body)
}

type ErrorResponse interface {
Message() string
}
Expand All @@ -191,12 +184,14 @@ func WithErrorResponse(resource ErrorResponse) DoOption {
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))
// to print the response, set the envvar BATON_DEBUG_PRINT_RESPONSE_BODY as non-empty, instead
return fmt.Errorf("unexpected content type for JSON error response: %s. status code: %d", contentHeader, resp.StatusCode)
}

// Decode the JSON response body into the ErrorResponse
if err := json.Unmarshal(resp.Body, &resource); err != nil {
return fmt.Errorf("failed to unmarshal JSON error response: %w. status code: %d. body %v", err, resp.StatusCode, logBody(resp.Body, maxBodySize))
// to print the response, set the envvar BATON_DEBUG_PRINT_RESPONSE_BODY as non-empty, instead
return fmt.Errorf("failed to unmarshal JSON error response: %w. status code: %d", err, resp.StatusCode)
}

// Construct a more detailed error message
Expand Down

0 comments on commit e9a3deb

Please sign in to comment.