Skip to content

Commit

Permalink
Merge pull request #18 from erubboli-bfx/master
Browse files Browse the repository at this point in the history
Add more errors handling
  • Loading branch information
prdn committed Apr 5, 2016
2 parents 1dd11b4 + 9af4ae5 commit f075c03
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
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

0 comments on commit f075c03

Please sign in to comment.