diff --git a/lib/bitfinex/errors.rb b/lib/bitfinex/errors.rb index ebda27c..2a87fce 100644 --- a/lib/bitfinex/errors.rb +++ b/lib/bitfinex/errors.rb @@ -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 diff --git a/lib/bitfinex/version.rb b/lib/bitfinex/version.rb index 36a011a..dc32bd4 100644 --- a/lib/bitfinex/version.rb +++ b/lib/bitfinex/version.rb @@ -1,3 +1,3 @@ module Bitfinex - VERSION = "0.0.5" + VERSION = "0.0.6" end diff --git a/spec/bitfinex/error_handling_spec.rb b/spec/bitfinex/error_handling_spec.rb index 88e7c8f..dfbb0ef 100644 --- a/spec/bitfinex/error_handling_spec.rb +++ b/spec/bitfinex/error_handling_spec.rb @@ -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 @@ -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