Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Distinguish errors by Exception class #47

Merged
merged 10 commits into from
Jul 30, 2021

Conversation

tim-wovn
Copy link
Contributor

@tim-wovn tim-wovn commented Jul 27, 2021

Recently Microsoft had wide outages across their various products. The translate API was unable to obtain the right credentials and failed with error Invalid credentials. The real issue was a 503 service unavailable.

This PR does 2 things:

  • distinguishes each unique error type by a new exception class. Users can rescue on those classes, rather than rescuing all errors and needing to check the contents of the error to determine what to do
  • catch and raise on 503 service unavailable

The first point is done in a backwards compatible way. Existing code that rescues on BingTranslator::Exception will still run as before. With the important exception of catching 503 errors, which have a new unique string associated with them.

I think the second point is redundant with #46 and the solution in the other PR is more generally useful.


https://status.azure.com/status/history/
Between approximately 07:00 UTC and 13:55 UTC on 21 Jul 2021, customers using Cognitive Services may have experienced difficulties connecting to resources. Impacted customers may have experienced timeouts or 401 authorization errors.

I personally saw 503 errors for the few cases I debugged but it could be that during the service downtime interval, various error codes were returned. Maybe catching only 503 is too specific?

@danopia
Copy link
Collaborator

danopia commented Jul 28, 2021

Hi, thanks for the PR!

For an "Unavailable" situation it's generally good to consider any 5xx code. I usually use number comparison for this in other languages, but given that Ruby apparently gives the status as a string, maybe .start_with?('5') is cleaner; I don't currently have a strong opinion..

The change in #46 is probably still relevant I think; and I can rebase it separately from this PR since your patchset doesn't include it.

@tim-wovn
Copy link
Contributor Author

tim-wovn commented Jul 28, 2021

@danopia Thank you for your review.

From your suggestion, I went and checked the library documentation:
Note that each possible HTTP response code defines its own HTTPResponse subclass. All classes are defined under the Net module. Indentation indicates inheritance.
https://ruby-doc.org/stdlib-2.6.3/libdoc/net/http/rdoc/Net/HTTPResponse.html

So rather than check the error code directly, I compare against the base class for 5XX errors. I then changed the if sequences to be a switch statement. I did this in f0ba154 If you prefer to check against strings, I can change to that.

I added some tests to validate the new behaviour in d42cc3a

And I merged in the changes from #46 (well, I merged with master to be specific) in 5671991

Please let me know what you think. Thank you!

@danopia
Copy link
Collaborator

danopia commented Jul 28, 2021

Excellent, the subclass handling looks like the correct way of handling these HTTP statuses! I'll do a CI run of this in a branch soon

Copy link
Collaborator

@danopia danopia left a comment

Choose a reason for hiding this comment

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

when Net::HTTPServerError
raise UnavailableException.new("#{response.code}: Credentials server unavailable")
else
raise Exception.new("Unsuccessful Access Token call: Code: #{response.code} (Invalid credentials?)")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe the specialized error class got lost here when merging master.

The new spec seems to expect AuthenticationException

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the sloppy merge. I've fixed this in 1f71d42 and added tests to ensure the correct exception is being raised. Thanks!

spec/bing_translator_spec.rb Show resolved Hide resolved
Copy link
Collaborator

@danopia danopia left a comment

Choose a reason for hiding this comment

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

spec/bing_translator_spec.rb Show resolved Hide resolved
@danopia
Copy link
Collaborator

danopia commented Jul 30, 2021

I'll go ahead and merge this, but I don't have the access to cut a release so that will still be pending. I will update here with a version number when it's done.

@danopia danopia merged commit 6c4d18b into relrod:master Jul 30, 2021
@tim-wovn
Copy link
Contributor Author

Thanks for your review and taking care of the merge!

@danopia
Copy link
Collaborator

danopia commented Aug 11, 2021

🎉 This PR is included in version 6.2.0 🎉

The release is available on:

Your friendly neighborhood human 📦🚀

@tim-wovn
Copy link
Contributor Author

Upgrading now! Thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants