workos-go icon indicating copy to clipboard operation
workos-go copied to clipboard

Always return `code` or `error` in responses as `ErrorCode` when present

Open mthadley opened this issue 2 years ago • 0 comments

Description

We currently set ErrorCode in HTTPError from the code field in raw responses from the WorkOS API, but only in certain cases, like getting a 422.

Otherwise, ErrorCode ends up as empty string in other cases. For example, as noted in #198, when an invalid Authorization Code is provided to the token endpoint, the following raw error is returned:

{ 
  "error": "invalid_grant", 
  "error_description": "The code '01E2RJ4C05B52KKZ8FSRDAP23J' has expired or is invalid." 
}

However, the ErrorCode is not populated, making it hard to handle this (or other) particular errors.

This PR updates the error parsing logic to always attempt to populate ErrorCode with code from the response, falling back to error when not present. In all cases I'm aware of, error should be a discrete error type identifier suitable for dispatching on in response handling code.

Documentation

Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.

[ ] Yes

If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.

mthadley avatar Apr 13 '23 19:04 mthadley