Skip to content

Commit f075c03

Browse files
committed
Merge pull request #18 from erubboli-bfx/master
Add more errors handling
2 parents 1dd11b4 + 9af4ae5 commit f075c03

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

lib/bitfinex/errors.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,25 @@ class ParamsError < ClientError; end
66
class InvalidAuthKeyError < ClientError; end
77
class BlockMissingError < ParamsError; end
88
class ServerError < Exception; end # Error reported back by Binfinex server
9+
class BadRequestError < ServerError; end
10+
class NotFoundError < ServerError; end
11+
class ForbiddenError < ServerError; end
12+
class UnauthorizedError < ServerError; end
13+
class InternalServerError < ServerError; end
914

1015
class CustomErrors < Faraday::Response::Middleware
1116
def on_complete(env)
1217
case env[:status]
13-
when 400..500
14-
raise ServerError, env.body['message']
18+
when 400
19+
raise BadRequestError, env.body['message']
20+
when 401
21+
raise UnauthorizedError
22+
when 403
23+
raise ForbiddenError
24+
when 404
25+
raise NotFoundError
26+
when 500
27+
raise InternalServerError
1528
else
1629
super
1730
end

lib/bitfinex/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Bitfinex
2-
VERSION = "0.0.5"
2+
VERSION = "0.0.6"
33
end

spec/bitfinex/error_handling_spec.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,31 @@
1616
stub_http("/pubticker/btcusd",{message: "error message 400"}.to_json,status: 400)
1717
end
1818

19-
it { expect{ client.ticker }.to raise_error(Bitfinex::ServerError) }
19+
it { expect{ client.ticker }.to raise_error(Bitfinex::BadRequestError, "error message 400") }
20+
end
21+
22+
context "401 error" do
23+
before do
24+
stub_http("/pubticker/btcusd",{message: "unauthorized 401"}.to_json,status: 401)
25+
end
26+
27+
it { expect{ client.ticker }.to raise_error(Bitfinex::UnauthorizedError) }
28+
end
29+
30+
context "403 error" do
31+
before do
32+
stub_http("/pubticker/btcusd",{message: "forbidden 403"}.to_json,status: 403)
33+
end
34+
35+
it { expect{ client.ticker }.to raise_error(Bitfinex::ForbiddenError) }
36+
end
37+
38+
context "404 error" do
39+
before do
40+
stub_http("/pubticker/btcusd",{message: "404 not found"}.to_json, status:404)
41+
end
42+
43+
it { expect{ client.ticker }.to raise_error(Bitfinex::NotFoundError) }
2044
end
2145

2246

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

28-
it { expect{ client.ticker }.to raise_error(Bitfinex::ServerError, "error message 500") }
52+
it { expect{ client.ticker }.to raise_error(Bitfinex::InternalServerError) }
2953
end
3054

3155
end

0 commit comments

Comments
 (0)