PagerDuty / go-pagerduty

go client library for PagerDuty v2 API
https://v2.developer.pagerduty.com/docs/rest-api
Apache License 2.0
288 stars 242 forks source link

Error Response Body being discarded #476

Open TCap103 opened 1 year ago

TCap103 commented 1 year ago

When using ManageEventWithContext if there is a response error we only get the error code and the high level error message back. For example: "HTTP response failed with status code 400 and no JSON error object was present".

From inspecting with curl I know there actually is a detailed error message. It appears that the APIError that we're attempting to decode into in client.getErrorFromResponse expects the response.Body to have the json key error however the body is actually (in my test example): {"errors":["'payload.source' is missing or blank"],"message":"Event object is invalid","status":"invalid event"}

I do not know if doWithEndpoint's other usages suffer from the same issue, or actually match the currently implemented behavior as I do not use those flows.

Currently working around this by using the debug methods to decode the response properly in my own code.

ChuckCrawford commented 1 year ago

@TCap103 : would you mind letting us know which API(s) you are working with that are returning an "errors" key?

adamdecaf commented 1 year ago

I am seeing this when sending a v2 event and payload with ManageEventWithContext. See https://github.com/moov-io/achgateway/issues/171 for our issue tracking this.

Code: https://github.com/moov-io/achgateway/blob/master/internal/alerting/pagerduty.go#L32

Edit: Should we use .CreateEventWithContext(ctx, event) instead of ManageEventWithContext? We're just looking to trigger pages for on-call staff.

Edit2: I'm seeing the DebugFlag options, but am not finding a response body or v2 error response. Is there a better option to get full details why a v2 event couldn't be created?

Sometimes I was able to get a Invalid routing key response in plain text (aka no JSON response object).

HTTP response with status code 400 does not contain Content-Type: application/json
ChuckCrawford commented 11 months ago

@adamdecaf : did you see improvement here after the introduction of this PR into v1.6.0 and beyond?

ChuckCrawford commented 11 months ago

Same for @TCap103 : did you see improvement here after the introduction of this PR into v1.6.0 and beyond?

pgalbavy-itrs commented 1 month ago

I am using 1.8.0 and seeing the same, in my case I was letting payload.source be empty. I ended up using the debug flags and that's how I found out. My debug output (sorry for the formatting):

2024-10-09T08:28:14+01:00 DEBUG: cmd/root.go:215 cmd.sendEvent() > pagerduty: resp: "HTTP/1.1 400 Bad Request\r\nContent-Length: 112\r\nAccess-Control-Allow-Credentials: true\r\nAccess-Control-Allow-Origin: \r\nAccess-Control-Expose-Headers: \r\nCache-Control: max-age=0, private, must-revalidate\r\nConnection: keep-alive\r\nContent-Type: application/json; charset=utf-8\r\nDate: Wed, 09 Oct 2024 07:28:14 GMT\r\nServer: nginx\r\nVary: Origin\r\nX-Request-Id: F_y4NPcv--KfmZ4JitUC\r\n\r\n{\"errors\":[\"'payload.source' is missing or blank\"],\"message\":\"Event object is invalid\",\"status\":\"invalid event\"}"
...
Error: HTTP response failed with status code 400 and no JSON error object was present

The first line is the body dump of the response, the following line the content of err printed as a plain string.

Edit: I also tried unwrapping the error and got:

2024-10-09T08:34:06+01:00 DEBUG: cmd/root.go:218 cmd.sendEvent() > pagerduty: aerr: pagerduty.EventsAPIV2Error{StatusCode:0, APIError:pagerduty.NullEventsAPIV2ErrorObject{Valid:false, ErrorObject:pagerduty.EventsAPIV2ErrorObject{Status:"", Message:"", Errors:[]string(nil)}}, message:""}