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

Fix JSON decode (errorObject) #63

Merged
merged 1 commit into from
Mar 9, 2017
Merged

Fix JSON decode (errorObject) #63

merged 1 commit into from
Mar 9, 2017

Conversation

heimweh
Copy link
Contributor

@heimweh heimweh commented Jan 30, 2017

This PR aims to solve the following error when trying to decode the JSON response:

Current:

Response did not contain formatted error: Could not decode JSON response: json: cannot unmarshal object into Go struct field errorObject.errors of type []string. HTTP response code: 400. Raw response: &{Status:400 Bad Request StatusCode:400 Proto:HTTP/1.1 ProtoMajor:1 ProtoMinor:1 Header:map[Date:[Mon, 30 Jan 2017 19:08:00 GMT] Content-Type:[application/json; charset=utf-8] Access-Control-Allow-Headers:[Authorization, Content-Type] X-Ua-Compatible:[IE=Edge,chrome=1] Connection:[keep-alive] Access-Control-Allow-Origin:[*] Access-Control-Max-Age:[1728000] Cache-Control:[no-cache] X-Request-Id:[acde0a61602cd216ae1c1d66831bf0ed] Server:[nginx] Status:[400 Bad Request] Access-Control-Allow-Methods:[GET, POST, PUT, DELETE, OPTIONS]] Body:0xc42018e480 ContentLength:-1 TransferEncoding:[chunked] Close:false Uncompressed:false Trailer:map[] Request:0xc4204f8100 TLS:0xc42009c370}

After:

Failed call API endpoint. HTTP response code: 400. Error: &{3001 Invalid Schedule map[rotation_layers.weekly_restrictions.duration_seconds:[Please use a shorter duration or just turn the recurrence off.]]}

Without this, one would have to actually call the API manually or via the API reference to figure out what the actual error was, this can hopefully solve most error cases.

Since API errors might come back a bit different (see below) I changed Errors to interface{} to prevent JSON decode errors. Also fixed a typo in errorObject.

Example error responses:

{
  "error": {
  "message": "Invalid Schedule",
  "code": 3001,
  "errors": {
    "rotation_layers.weekly_restrictions.duration_seconds": [
      "Please use a shorter duration or just turn the recurrence off."
    ]
  }
}
{
  "error": {
    "message": "Invalid Input Provided",
    "code": 2001,
    "errors": [
      "Time zone must be a valid time zone string."
    ]
  }
}

@heimweh heimweh changed the title Fix unmarshaling error and typo on errorObject Fix JSON decode (errorObject) Jan 30, 2017
@heimweh
Copy link
Contributor Author

heimweh commented Mar 2, 2017

@lfepp would you mind taking a look at this one?
this would help a lot when debugging configuration errors such as the ones above.

Maybe there's a better way of handling the API errors though, I'm not sure.

@lfepp lfepp self-requested a review March 8, 2017 01:28
@lfepp
Copy link
Contributor

lfepp commented Mar 8, 2017

@heimweh I'll assign this one to myself and will try to take a look shortly

@heimweh
Copy link
Contributor Author

heimweh commented Mar 8, 2017

@lfepp thanks! :)

Copy link
Contributor

@lfepp lfepp left a comment

Choose a reason for hiding this comment

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

LGTM

@lfepp
Copy link
Contributor

lfepp commented Mar 9, 2017

I love the update @heimweh, thanks for the contributions!

@lfepp lfepp merged commit d7f38f6 into PagerDuty:master Mar 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants