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

Handle invalid ChangeEvent APIErrorObject response #479

Merged
merged 1 commit into from
Dec 12, 2023
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
62 changes: 33 additions & 29 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,37 @@ type APIErrorObject struct {
Errors []string `json:"errors,omitempty"`
}

// fallbackAPIErrorObject is a shim to solve this issue:
// https://github.com/PagerDuty/go-pagerduty/issues/339
//
// TODO: remove when PagerDuty engineering confirms bugfix to the REST API
type fallbackAPIErrorObject struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Errors string `json:"errors,omitempty"`
func unmarshalApiErrorObject(data []byte) (APIErrorObject, error) {
var aeo APIErrorObject
err := json.Unmarshal(data, &aeo)
if err == nil {
return aeo, nil
}
if _, ok := err.(*json.UnmarshalTypeError); !ok {
return aeo, nil
}
// See - https://github.com/PagerDuty/go-pagerduty/issues/339
// TODO: remove when PagerDuty engineering confirms bugfix to the REST API
var fallback1 struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Errors string `json:"errors,omitempty"`
}
if json.Unmarshal(data, &fallback1) == nil {
aeo.Code = fallback1.Code
aeo.Message = fallback1.Message
aeo.Errors = []string{fallback1.Errors}
return aeo, nil
}
// See - https://github.com/PagerDuty/go-pagerduty/issues/478
var fallback2 []string
if json.Unmarshal(data, &fallback2) == nil {
aeo.Message = "none"
aeo.Errors = fallback2
return aeo, nil
}
// still failed, so return the original error
return aeo, err
}

// NullAPIErrorObject is a wrapper around the APIErrorObject type. If the Valid
Expand All @@ -111,28 +134,9 @@ var _ json.Unmarshaler = (*NullAPIErrorObject)(nil) // assert that it satisfies

// UnmarshalJSON satisfies encoding/json.Unmarshaler
func (n *NullAPIErrorObject) UnmarshalJSON(data []byte) error {
var aeo APIErrorObject

err := json.Unmarshal(data, &aeo)
aeo, err := unmarshalApiErrorObject(data)
if err != nil {
terr, ok := err.(*json.UnmarshalTypeError)
if !ok {
return err
}

//
// see https://github.com/PagerDuty/go-pagerduty/issues/339
//
var faeo fallbackAPIErrorObject

if err := json.Unmarshal(data, &faeo); err != nil {
// still failed, so return the original error
return terr
}

aeo.Code = faeo.Code
aeo.Message = faeo.Message
aeo.Errors = []string{faeo.Errors}
return err
}

n.ErrorObject = aeo
Expand Down
5 changes: 5 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ func TestAPIError_Error(t *testing.T) {
input: `{"error":{"code": 420, "message": "Enhance Your Calm", "errors":["No Seriously, Enhance Your Calm", "Slow Your Roll", "No, really..."]}}`,
want: "HTTP response failed with status code 429, message: Enhance Your Calm (code: 420): No Seriously, Enhance Your Calm (and 2 more errors...)",
},
{
name: "issue_478",
input: `{"error":["links should have at most 50 item(s)"]}`,
want: "HTTP response failed with status code 429, message: none (code: 0): links should have at most 50 item(s)",
},
}

for _, tt := range tests {
Expand Down