diff --git a/lib/faraday/raise_http_exception.rb b/lib/faraday/raise_http_exception.rb index c24e5af..38f3c1a 100644 --- a/lib/faraday/raise_http_exception.rb +++ b/lib/faraday/raise_http_exception.rb @@ -7,16 +7,21 @@ class RaiseHTTPException < Faraday::Middleware def call(env) @app.call(env).on_complete do |response| response_hash = JSON.parse(response.body) - msg = "#{response[:status]} #{response_hash['message']}" + msg = "#{response[:status]} #{response_hash['code']}: #{response_hash['message']}" case response[:status] when 400 ; raise Postmates::BadRequest, msg when 401 ; raise Postmates::Unauthorized, msg + when 402 ; raise Postmates::CustomerSuspended, msg when 403 ; raise Postmates::Forbidden, msg when 404 ; raise Postmates::NotFound, msg when 500 ; raise Postmates::InternalServerError, msg when 503 ; raise Postmates::ServiceUnavailable, msg end + + if response[:status] >= 300 + raise Postmates::Error, msg + end end end @@ -25,4 +30,4 @@ def initialize(app) @parser = nil end end -end \ No newline at end of file +end diff --git a/lib/postmates/error.rb b/lib/postmates/error.rb index 72bd3ce..944dd17 100644 --- a/lib/postmates/error.rb +++ b/lib/postmates/error.rb @@ -2,8 +2,9 @@ module Postmates class Error < StandardError; end # custom Postmates error class class BadRequest < Error; end # 400 class Unauthorized < Error; end # 401 + class CustomerSuspended < Error; end # 402 class Forbidden < Error; end # 403 class NotFound < Error; end # 404 class InternalServerError < Error; end # 500 class ServiceUnavailable < Error; end # 503 -end \ No newline at end of file +end diff --git a/spec/error_spec.rb b/spec/error_spec.rb index 7b4bcb8..1859faa 100644 --- a/spec/error_spec.rb +++ b/spec/error_spec.rb @@ -30,6 +30,18 @@ end end + describe 'when customer account is suspended' do + before do + stub_get(path_to('deliveries'), + response_code: 402, returns: 'service_unavailable.json') + end + + it 'raises Postmates::CustomerSuspended' do + expect { client.list } + .to raise_error Postmates::CustomerSuspended + end + end + describe 'Forbidden' do let(:bad_client) { Postmates.new } before do @@ -78,5 +90,17 @@ .to raise_error Postmates::ServiceUnavailable end end + + describe 'with an unknown error' do + before do + stub_get(path_to('deliveries'), + response_code: 504, returns: 'unknown.json') + end + + it 'raises Postmates::Error' do + expect { client.list } + .to raise_error Postmates::Error + end + end end -end \ No newline at end of file +end diff --git a/spec/fixtures/suspended.json b/spec/fixtures/suspended.json new file mode 100644 index 0000000..d08b631 --- /dev/null +++ b/spec/fixtures/suspended.json @@ -0,0 +1,5 @@ +{ + "kind": "error", + "code": "customer_suspended", + "message": "engineering@yourcompany.com" +} diff --git a/spec/fixtures/unknown.json b/spec/fixtures/unknown.json new file mode 100644 index 0000000..8925ee3 --- /dev/null +++ b/spec/fixtures/unknown.json @@ -0,0 +1,5 @@ +{ + "kind": "error", + "code": "unknown", + "message": "something blew up" +}