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

feat(cfapi): include Ray ID in signing errors #111

Merged
merged 1 commit into from
Jan 21, 2024
Merged
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
9 changes: 7 additions & 2 deletions internal/cfapi/cfapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ type APIResponse struct {
type APIError struct {
Code int `json:"code"`
Message string `json:"message"`
RayID string `json:"-"`
}

func (a *APIError) Error() string {
return fmt.Sprintf("Cloudflare API Error code=%d message=%s", a.Code, a.Message)
return fmt.Sprintf("Cloudflare API Error code=%d message=%s ray_id=%s", a.Code, a.Message, a.RayID)
}

func (c *Client) Sign(ctx context.Context, req *SignRequest) (*SignResponse, error) {
Expand All @@ -108,13 +109,17 @@ func (c *Client) Sign(ctx context.Context, req *SignRequest) (*SignResponse, err
}
defer resp.Body.Close()

rayID := resp.Header.Get("CF-Ray")

api := APIResponse{}
if err := json.NewDecoder(resp.Body).Decode(&api); err != nil {
return nil, err
}

if !api.Success {
return nil, &api.Errors[0]
err := &api.Errors[0]
err.RayID = rayID
Copy link
Contributor Author

@terinjokes terinjokes Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other idea I had was to keep APIError unchanged and return a wrapped error instead. Thoughts?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like all of the cloudflare-go errors include a RayID field, so I think you're fine to keep doing the same thing here.

return nil, err
}

signResp := SignResponse{}
Expand Down
4 changes: 3 additions & 1 deletion internal/cfapi/cfapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func TestSign(t *testing.T) {
}{
{name: "API success",
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("cf-ray", "0123456789abcdef-ABC")
fmt.Fprintln(w, `{
"success": true,
"errors": [],
Expand Down Expand Up @@ -109,6 +110,7 @@ func TestSign(t *testing.T) {
{
name: "API error",
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("cf-ray", "0123456789abcdef-ABC")
fmt.Fprintln(w, `{
"success": false,
"errors": [{"code": 9001, "message": "Over Nine Thousand!"}],
Expand All @@ -117,7 +119,7 @@ func TestSign(t *testing.T) {
}`)
}),
response: nil,
error: "Cloudflare API Error code=9001 message=Over Nine Thousand!",
error: "Cloudflare API Error code=9001 message=Over Nine Thousand! ray_id=0123456789abcdef-ABC",
},
}

Expand Down
Loading