Skip to content
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

Show trace on error #391

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
47 changes: 30 additions & 17 deletions rest/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,45 @@ func (c *Client) CallURL(ctx context.Context, method, uri string, response any,
req.SetResult(response).SetError(&models.ErrorResponse{})

res, err := req.Execute(method, uri)
if err != nil {
return fmt.Errorf("failed to execute request: %w", err)
} else if res.IsError() {
errRes := res.Error().(*models.ErrorResponse)
errRes.StatusCode = res.StatusCode()
if errRes.RequestID == "" {
errRes.RequestID = res.Header().Get("X-Request-ID")
if err != nil || res.IsError() {
if options.Trace {
c.logRequestTrace(uri, req, res)
}
return errRes
}

if options.Trace {
fmt.Printf("Request URL: %s\n", uri)
sanitizedHeaders := req.Header
for k := range sanitizedHeaders {
if k == "Authorization" {
sanitizedHeaders[k] = []string{"REDACTED"}
if err != nil {
return fmt.Errorf("failed to execute request: %w", err)
} else {
errRes := res.Error().(*models.ErrorResponse)
errRes.StatusCode = res.StatusCode()
if errRes.RequestID == "" {
errRes.RequestID = res.Header().Get("X-Request-ID")
}
return errRes
}
fmt.Printf("Request Headers: %s\n", sanitizedHeaders)
fmt.Printf("Response Headers: %+v\n", res.Header())
}

if options.Trace {
c.logRequestTrace(uri, req, res)
}

return nil
}

func (c *Client) logRequestTrace(uri string, req *resty.Request, res *resty.Response) {
fmt.Printf("Request URL: %s\n", uri)
sanitizedHeaders := req.Header
for k := range sanitizedHeaders {
if k == "Authorization" {
sanitizedHeaders[k] = []string{"REDACTED"}
}
}
fmt.Printf("Request Headers: %s\n", sanitizedHeaders)
if res != nil {
fmt.Printf("Response Status Code: %d\n", res.StatusCode())
fmt.Printf("Response Headers: %+v\n", res.Header())
}
}

func mergeOptions(opts ...models.RequestOption) *models.RequestOptions {
options := &models.RequestOptions{}
for _, o := range opts {
Expand Down
Loading