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

Add more errors handling #18

Merged
merged 1 commit into from
Apr 5, 2016
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
17 changes: 15 additions & 2 deletions lib/bitfinex/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,25 @@ class ParamsError < ClientError; end
class InvalidAuthKeyError < ClientError; end
class BlockMissingError < ParamsError; end
class ServerError < Exception; end # Error reported back by Binfinex server
class BadRequestError < ServerError; end
class NotFoundError < ServerError; end
class ForbiddenError < ServerError; end
class UnauthorizedError < ServerError; end
class InternalServerError < ServerError; end

class CustomErrors < Faraday::Response::Middleware
def on_complete(env)
case env[:status]
when 400..500
raise ServerError, env.body['message']
when 400
raise BadRequestError, env.body['message']
when 401
raise UnauthorizedError
when 403
raise ForbiddenError
when 404
raise NotFoundError
when 500
raise InternalServerError
else
super
end
Expand Down
2 changes: 1 addition & 1 deletion lib/bitfinex/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Bitfinex
VERSION = "0.0.5"
VERSION = "0.0.6"
end
28 changes: 26 additions & 2 deletions spec/bitfinex/error_handling_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,31 @@
stub_http("/pubticker/btcusd",{message: "error message 400"}.to_json,status: 400)
end

it { expect{ client.ticker }.to raise_error(Bitfinex::ServerError) }
it { expect{ client.ticker }.to raise_error(Bitfinex::BadRequestError, "error message 400") }
end

context "401 error" do
before do
stub_http("/pubticker/btcusd",{message: "unauthorized 401"}.to_json,status: 401)
end

it { expect{ client.ticker }.to raise_error(Bitfinex::UnauthorizedError) }
end

context "403 error" do
before do
stub_http("/pubticker/btcusd",{message: "forbidden 403"}.to_json,status: 403)
end

it { expect{ client.ticker }.to raise_error(Bitfinex::ForbiddenError) }
end

context "404 error" do
before do
stub_http("/pubticker/btcusd",{message: "404 not found"}.to_json, status:404)
end

it { expect{ client.ticker }.to raise_error(Bitfinex::NotFoundError) }
end


Expand All @@ -25,7 +49,7 @@
stub_http("/pubticker/btcusd",{message: "error message 500"}.to_json, status:500)
end

it { expect{ client.ticker }.to raise_error(Bitfinex::ServerError, "error message 500") }
it { expect{ client.ticker }.to raise_error(Bitfinex::InternalServerError) }
end

end