refactor: read GraphQL errors from the typed SDK error - #280
Open
ianaya89 wants to merge 1 commit into
Open
Conversation
ParseError unmarshalled err.Error() into a mirror of clientv2.ErrorResponse.
That string is itself json.Marshal(er), so every classification round-tripped
through the error message and only worked while the outermost error was the
SDK's own. Wrapping it anywhere broke everything downstream:
FormatError(fmt.Errorf("executing query: %w", resp), "env")
before: executing query: {"networkErrors":null,"graphqlErrors":[...]}
after: Not Found: env not found
IsNotFoundError(same)
before: (false, error parsing: invalid character 'e' ...)
after: (true, nil)
Resolve the error with errors.As instead, the way isRetryable already does two
files over, and project it into ClientError so the rest of the file stays put.
Path elements arrive as ast.PathName/ast.PathIndex rather than the string and
float64 the JSON round-trip produced, so they are flattened for errorPrefix.
NetworkError is typed now, which drops the string and map branches of
formatNetworkErrors; a network error with no message reports its status code
instead of nothing.
The tests here and in env/common hand-wrote the SDK's JSON. They now build real
clientv2.ErrorResponse values, so these paths are covered as they are called.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ParseErrorunmarshallederr.Error()into a hand-written mirror ofclientv2.ErrorResponse:That string is itself
json.Marshal(er)—ErrorResponse.Error()does exactly that. So every classification in the provider round-tripped through the error message, and only worked while the outermost error was the SDK's own.It is not just aesthetic
Wrapping the error anywhere breaks the whole chain. Measured on master:
A wrapped not-found stops being recognised, so
Readwould surface a raw JSON blob instead of removing the resource from state. Nothing wraps it today, which is the only reason this is latent — butWithRetrysits directly in that path, anderrors.Asis what stops the next wrap from being a silent regression.Change
errors.As(err, &errResp), the wayisRetryableinclient.goalready does two files over. The result is projected intoClientErrorsoFormatError,IsNotFoundError,IsActiveClustersErroranderrorPrefixare untouched —ClientErrorstops being a JSON-parse target and becomes a deliberate projection, keeping the SDK's shape out of the rest of the file.Two knock-on details:
ast.PathName/ast.PathIndex, not thestring/float64the JSON round-trip produced.errorPrefixtype-asserts onstring, sopathElementsflattens them. There is a test with an index in the path.NetworkErroris typed (*clientv2.HTTPError), which removes thestringandmap[string]interface{}branches offormatNetworkErrors— a string network error was never representable. A network error with an empty message now reportsNetwork error: HTTP 503instead ofNetwork error:.Tests
errors_test.goand two tests inenv/commonhand-wrote the SDK's JSON intoerrors.New. They now build realclientv2.ErrorResponsevalues, sowaitForDeletionandFormatDeleteErrorare exercised against the shape they actually receive. Added cases for a wrapped error response, an indexed path, a network error without a message, and a nil entry in the error list.Overlap
Touches the same file as #269 and #270 but different functions, so the source hunks are disjoint.
errors_test.gois rewritten here, so whichever merges second will want this version of that file.Not done, worth a follow-up: every caller of
IsNotFoundErrorandIsActiveClustersErrordiscards the returnederror(notFound, _ := ...), so both could drop it and return a plain bool.