Skip to content

Commit

Permalink
Merge pull request #76 from mattbeedle/fix-errors
Browse files Browse the repository at this point in the history
Fix errors
  • Loading branch information
mattbeedle committed Sep 9, 2014
2 parents 4298f94 + 3c16584 commit 51af5c4
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

notification :tmux

guard 'rspec', all_on_start: true, all_after_pass: true, failed_mode: :keep do
guard 'rspec', cmd: 'rspec', all_on_start: true, all_after_pass: true,
failed_mode: :keep do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
Expand Down
4 changes: 4 additions & 0 deletions lib/capsule_crm/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def error_constants
end
end

def error_from_env(env)
class_for_error_code(env[:status]).new(env)
end

def class_name_for_error_name(name)
name.to_s.titleize.gsub(' ', '')
end
Expand Down
11 changes: 9 additions & 2 deletions lib/capsule_crm/faraday/middleware/raise_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ module CapsuleCRM
module Faraday
module Middleware
class RaiseError < ::Faraday::Response::Middleware
attr_writer :error_factory

def on_complete(env)
return if env[:status] < 400
raise CapsuleCRM::Errors.class_for_error_code(env[:status]).
new(env[:response])
raise error_factory.error_from_env(env)
end

private

def error_factory
@error_factory ||= CapsuleCRM::Errors
end
end
end
Expand Down
41 changes: 41 additions & 0 deletions spec/lib/capsule_crm/errors_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper'

describe CapsuleCRM::Errors do
describe '.error_from_env' do
let(:env) { OpenStruct.new(body: { message: 'test' }.to_json) }

subject { described_class.error_from_env(env) }

context 'with a 400 response' do
before { env.status = 400 }

it 'should return a bad request error' do
expect(subject).to be_a(CapsuleCRM::Errors::BadRequest)
end
end

context 'with a 401 response' do
before { env.status = 401 }

it 'should return a not authorized error' do
expect(subject).to be_a(CapsuleCRM::Errors::NotAuthorized)
end
end

context 'with a 404 response' do
before { env.status = 404 }

it 'should return a not found error' do
expect(subject).to be_a(CapsuleCRM::Errors::NotFound)
end
end

context 'with a 500 response' do
before { env.status = 500 }

it 'should return a internal server error' do
expect(subject).to be_a(CapsuleCRM::Errors::InternalServerError)
end
end
end
end
59 changes: 59 additions & 0 deletions spec/lib/capsule_crm/faraday/middleware/raise_error_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'spec_helper'

describe CapsuleCRM::Faraday::Middleware::RaiseError do
describe '#on_complete' do
subject { described_class.new }

context 'when the response is a success' do
let(:response) { Hash[status: 200] }

it 'should return nothing' do
expect(subject.on_complete(response)).to eql(nil)
end
end

context 'when the response is a 400' do
let(:response) do
OpenStruct.new(status: 400, body: { message: 'test' }.to_json)
end

it 'should raise a bad request error' do
expect { subject.on_complete(response) }.
to raise_error(CapsuleCRM::Errors::BadRequest)
end
end

context 'when the response is a 401' do
let(:response) do
OpenStruct.new(status: 401, body: { message: 'test' }.to_json)
end

it 'should raise a not authorized error' do
expect { subject.on_complete(response) }.
to raise_error(CapsuleCRM::Errors::NotAuthorized)
end
end

context 'when the response is a 404' do
let(:response) do
OpenStruct.new(status: 404, body: { message: 'test' }.to_json)
end

it 'should raise a not found error' do
expect { subject.on_complete(response) }.
to raise_error(CapsuleCRM::Errors::NotFound)
end
end

context 'when the response is a 500' do
let(:response) do
OpenStruct.new(status: 500, body: { message: 'test' }.to_json)
end

it 'should raise an internal server error' do
expect { subject.on_complete(response) }.
to raise_error(CapsuleCRM::Errors::InternalServerError)
end
end
end
end

0 comments on commit 51af5c4

Please sign in to comment.