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

API error responses no longer contain error code explanations #327

Closed
metskem opened this issue May 3, 2021 · 6 comments
Closed

API error responses no longer contain error code explanations #327

metskem opened this issue May 3, 2021 · 6 comments

Comments

@metskem
Copy link

metskem commented May 3, 2021

A couple of months ago I updated to the 1.4.0 release of the pagerduty golang library.
I noticed that error responses now have become more cryptic, and leaves us basically in the dark.
I have 2 examples with the response from 1.3.0 and 1.4.0:

1.3.0:
Failed call API endpoint. HTTP response code: 400. Error: &{2001 Invalid Input Provided [User cannot be removed as they belong to an escalation policy on this team]}
1.4.0:
HTTP response failed with status code 400, message: Invalid Input Provided (code: 2001)

1.3.0:
Failed call API endpoint. HTTP response code: 400. Error: &{2001 Invalid Input Provided [User cannot be removed as they belong to an escalation policy on this team]}
1.4.0:
HTTP response failed with status code 400, message: Invalid Input Provided (code: 2001)

So basically, both errors are summarized as "Invalid Input Provided (code: 2001)", which leaves us clueless.

For now I reverted back to 1.3.0 (we were not yet using Tag support).

Would it be possible to fix this, and give more clear error responses again?

@theckman
Copy link
Collaborator

theckman commented May 3, 2021

@metskem I can take a look at that this week. Alternatively, if you're able to dig-in and raise a PR against the release-1.4.x branch I can review+merge it. It's a little less friction for me to merge someone else's change than to have my own merged because of repo permissions. 😄

Separately/In the mean time, are you able to use the APIError field here to get that additional context: https://pkg.go.dev/github.com/PagerDuty/go-pagerduty#APIError

Also, v1.4.0 is only a few days old and this new API error handling was added then.

@theckman
Copy link
Collaborator

theckman commented May 3, 2021

Actually, the v1.5.0 tree hasn't been started on master yet. This means we can raise the PR against master, and I'll make sure it gets onto the release-1.4.x branch.

@metskem
Copy link
Author

metskem commented May 3, 2021

@metskem I can take a look at that this week. Alternatively, if you're able to dig-in and raise a PR against the release-1.4.x branch I can review+merge it. It's a little less friction for me to merge someone else's change than to have my own merged because of repo permissions. 😄

Separately/In the mean time, are you able to use the APIError field here to get that additional context: https://pkg.go.dev/github.com/PagerDuty/go-pagerduty#APIError

Also, v1.4.0 is only a few days old and this new API error handling was added then.

I can use APIError like in

		err = pd.DeleteUser("notthere")
		if err != nil {
			var aerr pagerduty.APIError
			if errors.As(err, &aerr) {
				log.Printf("aerror: %v", aerr)

But then still I don't know how to get the detailed error explanation (I might be missing something here).

@theckman
Copy link
Collaborator

theckman commented May 4, 2021

@metskem Assuming we are inside of that errors.As() if block:

if aerr.APIError.Valid {
  fmt.Printf"top-level error message and code: %s (%d)\n", aerr.APIError.ErrorObject.Message, aerr.APIError.ErrorObject.Code)

  // get all error strings
  for i, errStr := range aerr.APIError.ErrorObject.Errors {
    fmt.Printf("error %d: %s\n", i, errStr)
  }
}

As of this moment, the error string printed does not include those additional error strings from the API. We could capture the first error string in the response, but it'd still be on consumers to pick the error apart and show the rest.

@metskem
Copy link
Author

metskem commented May 4, 2021

That works perfect Tim. Thanks for the quick response, I appreciate.
I guess we can close the issue now.

@metskem metskem closed this as completed May 4, 2021
theckman added a commit that referenced this issue May 16, 2021
While refactoring the API handling with the `APIError` type, we gave callers
more capability to inspect the errors that come from the API so that we can
handle them. Unfortunately, in the process we lost some fidelity in the strings
returned by its `Error()` method, compared to how errors used to be handled.

This updates the `APIError.Error()` method to now include the first detailed
error string provided from the API, if there is one, and incidate how many more
existed in the response (without printing them). This should help keep the error
tidy, while making it actionable by consumers. Consumers of this package are
still encouraged to deeply inspect the error, to handle each one.

This was inspired by #327, which the reported closed after learning how to
deeply inspect the returned error value.
theckman added a commit that referenced this issue May 16, 2021
While refactoring the API handling with the `APIError` type, we gave callers
more capability to inspect the errors that come from the API so that we can
handle them. Unfortunately, in the process we lost some fidelity in the strings
returned by its `Error()` method, compared to how errors used to be handled.

This updates the `APIError.Error()` method to now include the first detailed
error string provided from the API, if there is one, and incidate how many more
existed in the response (without printing them). This should help keep the error
tidy, while making it actionable by consumers. Consumers of this package are
still encouraged to deeply inspect the error, to handle each one.

This was inspired by #327, which the reporter closed after learning how to
deeply inspect the returned error value.
@theckman
Copy link
Collaborator

I raised #334, which should help add some fidelity back to the error strings. It'll be part of v1.5.0 once it's released.

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

No branches or pull requests

2 participants