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

Refactored out the custom way of preserving an original exception #1

Merged
merged 1 commit into from
Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions lib/pwned/error.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
module Pwned
class Error < StandardError
attr_reader :original_error

def initialize(message, original_error)
@original_error = original_error
super(message)
end
end

class TimeoutError < Error
end
end
end
18 changes: 8 additions & 10 deletions lib/pwned/password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,19 @@ def hashes
end

def get_hashes
begin
open("#{API_URL}#{hashed_password[0..(HASH_PREFIX_LENGTH-1)]}", @request_options) do |io|
@hashes = io.read
end
@hashes
rescue Timeout::Error => e
raise Pwned::TimeoutError.new(e.message, e)
rescue => e
raise Pwned::Error.new(e.message, e)
open("#{API_URL}#{hashed_password[0..(HASH_PREFIX_LENGTH-1)]}", @request_options) do |io|
@hashes = io.read
end
@hashes
rescue Timeout::Error => e
raise Pwned::TimeoutError, e.message
rescue => e
raise Pwned::Error, e.message
end

def match_data
@match_data ||= hashes.match(/#{hashed_password[HASH_PREFIX_LENGTH..-1]}:(\d+)/)
end

end
end
end
38 changes: 31 additions & 7 deletions spec/pwned/password_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,18 @@
end

it "raises a custom error" do
expect { password.pwned? }.to raise_error(Pwned::TimeoutError)
expect { password.pwned_count }.to raise_error(Pwned::TimeoutError)
expect { password.pwned? }.to raise_error(&method(:verify_timeout_error))
expect { password.pwned_count }.to raise_error(&method(:verify_timeout_error))
expect(@stub).to have_been_requested.times(2)
end

def verify_timeout_error(error)
aggregate_failures "testing custom error" do
expect(error).to be_kind_of(Pwned::TimeoutError)
expect(error.message).to match(/execution expired/)
expect(error.cause).to be_kind_of(Net::OpenTimeout)
end
end
end

describe "when the API returns an error" do
Expand All @@ -64,10 +72,18 @@
end

it "raises a custom error" do
expect { password.pwned? }.to raise_error(Pwned::Error)
expect { password.pwned_count }.to raise_error(Pwned::Error)
expect { password.pwned? }.to raise_error(&method(:verify_internal_error))
expect { password.pwned_count }.to raise_error(&method(:verify_internal_error))
expect(@stub).to have_been_requested.times(2)
end

def verify_internal_error(error)
aggregate_failures "testing custom error" do
expect(error).to be_kind_of(Pwned::Error)
expect(error.message).to match(/500/)
expect(error.cause).to be_kind_of(OpenURI::HTTPError)
end
end
end

describe "when the API returns a 404" do
Expand All @@ -77,10 +93,18 @@
end

it "raises a custom error" do
expect { password.pwned? }.to raise_error(Pwned::Error)
expect { password.pwned_count }.to raise_error(Pwned::Error)
expect { password.pwned? }.to raise_error(&method(:verify_not_found_error))
expect { password.pwned_count }.to raise_error(&method(:verify_not_found_error))
expect(@stub).to have_been_requested.times(2)
end

def verify_not_found_error(error)
aggregate_failures "testing custom error" do
expect(error).to be_kind_of(Pwned::Error)
expect(error.message).to match(/404/)
expect(error.cause).to be_kind_of(OpenURI::HTTPError)
end
end
end

describe "advanced requests" do
Expand All @@ -106,4 +130,4 @@
to have_been_made.once
end
end
end
end