Skip to content

Conversation

@bitgopatmcl
Copy link
Contributor

Should address #137

Also it brings in PathReporter which has a bug as per #138, but we were calling JSON.stringify on the error anyway so it doesn't introduce a regression. Will try to sort that out in a follow-up.

@bitgopatmcl bitgopatmcl requested a review from a team July 8, 2022 15:48
Copy link
Contributor

@ericcrosson-bitgo ericcrosson-bitgo left a comment

Choose a reason for hiding this comment

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

Looks fine to me, I don't see any breaking changes.

I quite dislike Errors and especially subclassing Errors, but decodeExpecting is designed to throw and there's no better value to throw than an Error.

Lastly, I want to make sure this change identifies the entirety of #137. I see

The error message would be more helpful if it included a stringified version of the decoded error response

as best I can tell this is the main ask, so looks like this PR hits the 🎯

@jonball4
Copy link
Contributor

jonball4 commented Jul 8, 2022

Looks like a good improvement to me, although not exactly what I had in mind - do you think the "path-reported" decode error against the expected status code is clear enough to show the erroneous response from the API? I worry that the decode error will obfuscate it a little too much. I was hoping to see the value of the response. Perhaps we could just add that into the decode error? What do you think?

@ericcrosson-bitgo
Copy link
Contributor

ericcrosson-bitgo commented Jul 8, 2022

Oh, I think I see what you mean. So instead of saying "I tried to decode 200Codec and got these errors", it's sufficient to say "I expected a 200 but got this status code and response instead"? The downside of the former being a lot of noise when a simpler response would convey the same information

@bitgopatmcl
Copy link
Contributor Author

bitgopatmcl commented Jul 8, 2022

Maybe it would be helpful to list out the possibilities and what should happen:

  • Expected status code, decoded correctly: nothing thrown, result returned like normal
  • Unexpected status code, decoded correctly: error thrown: "Unexpected response 404: { message: "Thing not found" }"
  • Expected status code, decode errors: error thrown: "Could not decode response 200: { stuff: [ ... ] }"
  • Unexpected status code, decode errors: error thrown: "Unexpected response 404: { error: "oops" }"
  • Unknown status code: error thrown: "Unexpected response 418: { message: "I'm a teapot" }"

Do these seem right?

@bitgopatmcl bitgopatmcl force-pushed the add-original-decoded-request-to-superagent-error branch from 44d9f80 to e9537c1 Compare July 18, 2022 20:53
@ericcrosson-bitgo
Copy link
Contributor

ericcrosson-bitgo commented Jul 19, 2022

Maybe it would be helpful to list out the possibilities and what should happen:

I hope we have tests for each case, I'll take a look

Update: it appears we do

Copy link
Contributor

@ericcrosson-bitgo ericcrosson-bitgo left a comment

Choose a reason for hiding this comment

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

This change looks fine to me, it modifies decodeExpecting to return a particular error (DecodeError -- note @bitgopatmcl that this may be ambiguous with an io-ts decode error outside of an api-ts context) so we can identify this case with instanceof.

Additionally, change the error string in the message of the DecodeError to be more useful as determined by our users.

I dig it

.catch((err) => (err instanceof DecodeError ? err.message : ''));

assert.isTrue(result);
assert.deepEqual(result, 'Could not decode response 200: {"invalid":"response"}');
Copy link
Contributor

Choose a reason for hiding this comment

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

deepEqual an odd choice here!

Copy link
Contributor

Choose a reason for hiding this comment

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

} else if (res.status === 'decodeError') {
const error = `Could not decode response ${String(
res.original.status,
)}: ${JSON.stringify(res.original.body)}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that JSON.stringify can also throw 😭 I believe when a circular reference is encountered.

fp-ts provides a wrapper that returns an Either, which we may want to use

Copy link
Contributor

Choose a reason for hiding this comment

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

hurm how would you want to handle this case, @bitgopatmcl? It's pretty gnarly

I was about to open a ticket to track and then I had the thought -- is it even possible for res.original.body to contain a circular reference here? Meaning, was res.original.body recently JSON.parsed? If so, then I suppose there's no cause for concern (though I'll leave a note indicating that we've considered the unsafe use of JSON.stringify and determined that we're in the clear)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What about if the server returns a non-application/json response? We should probably consider that case too.

Comment on lines +160 to +162
const error = `Unexpected response ${String(
res.original.status,
)}: ${JSON.stringify(res.original.body)}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is res.original.status not already coerced to a string by the backticks?

Suggested change
const error = `Unexpected response ${String(
res.original.status,
)}: ${JSON.stringify(res.original.body)}`;
const error = `Unexpected response ${res.original.status}: ${JSON.stringify(res.original.body)}`;

Copy link
Contributor

Choose a reason for hiding this comment

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

@ericcrosson-bitgo ericcrosson-bitgo merged commit 9048a33 into BitGo:beta Jul 19, 2022
ericcrosson-bitgo added a commit to ericcrosson-bitgo/api-ts that referenced this pull request Jul 19, 2022
@github-actions
Copy link

🎉 This PR is included in version 0.2.0-beta.13 🎉

The release is available on npm package (@beta dist-tag)

Your semantic-release bot 📦🚀

@github-actions
Copy link

🎉 This PR is included in version 1.0.0-beta.18 🎉

The release is available on npm package (@beta dist-tag)

Your semantic-release bot 📦🚀

@jonball4
Copy link
Contributor

🍾 thank you guys

@github-actions
Copy link

🎉 This PR is included in version 0.2.0-beta.7 🎉

The release is available on npm package (@beta dist-tag)

Your semantic-release bot 📦🚀

@github-actions
Copy link

github-actions bot commented Aug 4, 2022

🎉 This PR is included in version 0.2.0-beta.10 🎉

The release is available on npm package (@beta dist-tag)

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants