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

Tag exceptions #106

Merged
merged 2 commits into from
Sep 4, 2017
Merged

Tag exceptions #106

merged 2 commits into from
Sep 4, 2017

Conversation

dentarg
Copy link
Collaborator

@dentarg dentarg commented Nov 5, 2016

Make it easy to rescue any error from Twingly::URL.parse with rescue Twingly::URL::Error.

Close #31.

Make it easy to rescue any error from Twingly::URL.parse with
"rescue Twingly::URL::Error".

Close #31.
@dentarg
Copy link
Collaborator Author

dentarg commented Nov 5, 2016

Not sure how to write a test for this... :)

But I think it works:

$ git d
diff --git a/lib/twingly/url.rb b/lib/twingly/url.rb
index d62e70e..af39394 100644
--- a/lib/twingly/url.rb
+++ b/lib/twingly/url.rb
@@ -27,9 +27,13 @@ module Twingly

     class << self
       def parse(potential_url)
+        0/0
         internal_parse(potential_url)
       rescue Twingly::URL::Error, Twingly::URL::Error::ParseError => error
         NullURL.new
+      rescue Exception => error
+        error.extend(Twingly::URL::Error)
+        raise
       end

       def internal_parse(potential_url)
$ bundle console
Resolving dependencies...
[1] pry(main)> begin
[1] pry(main)*   Twingly::URL.parse("foo")
[1] pry(main)* rescue Twingly::URL::Error => error
[1] pry(main)*   warn "Error in Twingly::URL"
[1] pry(main)* end
Error in Twingly::URL
=> nil
[2] pry(main)> error
=> #<ZeroDivisionError: divided by 0>
[3] pry(main)> Twingly::URL.parse("foo")
ZeroDivisionError: divided by 0
from /Users/dentarg/twingly/ruby/twingly-url/lib/twingly/url.rb:30:in `/'
[4] pry(main)> wtf??
Exception: ZeroDivisionError: divided by 0
--
 0: /Users/dentarg/twingly/ruby/twingly-url/lib/twingly/url.rb:30:in `/'
 1: /Users/dentarg/twingly/ruby/twingly-url/lib/twingly/url.rb:30:in `parse'

@dentarg dentarg mentioned this pull request Nov 5, 2016
@walro
Copy link
Contributor

walro commented Nov 6, 2016

Not sure how to write a test for this... :)

Maybe stubbing .internal_parse to throw some error could work for a test case

@walro
Copy link
Contributor

walro commented Nov 6, 2016

Could work, but it somewhat couples tests with implementation (you need to know that internal_parse is called by parse). Personally I think the benefit outweighs the drawback.

diff --git a/spec/lib/twingly/url_spec.rb b/spec/lib/twingly/url_spec.rb
index a45ba57..ffc420d 100644
--- a/spec/lib/twingly/url_spec.rb
+++ b/spec/lib/twingly/url_spec.rb
@@ -93,6 +93,18 @@ describe Twingly::URL do
       end
     end

+    context "when re-reraising errors" do
+      before do
+        allow(described_class)
+          .to receive(:internal_parse)
+          .and_raise(ZeroDivisionError)
+      end
+
+      it "always tags the error" do
+         expect { subject }.to raise_error(Twingly::URL::Error)
+      end
+    end
+
     context "when given bad input" do
       invalid_urls.each do |invalid_url|
         it "returns a NullURL for \"#{invalid_url}\"" do

@dentarg dentarg changed the title Tag exceptions [WIP] Tag exceptions Nov 6, 2016
@dentarg dentarg changed the title [WIP] Tag exceptions Tag exceptions Sep 4, 2017
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

Successfully merging this pull request may close these issues.

3 participants