-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Always raise a FrederickAPI::V2::Errors::Error if result set or resou…
…rce contains errors when making a request
- Loading branch information
Aaron Severs
committed
Dec 13, 2017
1 parent
f2fc832
commit d43c772
Showing
9 changed files
with
317 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module FrederickAPI | ||
module V2 | ||
module Errors | ||
# Base exception class for client errors (i.e. validation, bad request) | ||
class Error < JsonApiClient::Errors::ClientError | ||
attr_reader :errors | ||
|
||
# Initialize with a JsonApiClient::ResultSet or a Resource | ||
def initialize(result) | ||
# @env is used in base class JsonApiClient::Errors::Error | ||
@env = result | ||
@errors = result.errors || [] | ||
end | ||
|
||
def to_s | ||
return "Client Error: #{self.errors.first['detail']}" if self.errors.any? | ||
super | ||
end | ||
end | ||
|
||
class BadRequest < Error; end | ||
class UnprocessableEntity < Error; end | ||
|
||
ERROR_CODES = { | ||
'400' => BadRequest, | ||
'422' => UnprocessableEntity | ||
}.freeze | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
|
||
module FrederickAPI | ||
# Current gem version | ||
VERSION = '0.3.2' | ||
VERSION = '0.4.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe FrederickAPI::V2::Errors do | ||
it 'ERROR_CODES' do | ||
expect(described_class::ERROR_CODES).to eq( | ||
'400' => FrederickAPI::V2::Errors::BadRequest, | ||
'422' => FrederickAPI::V2::Errors::UnprocessableEntity | ||
) | ||
end | ||
end | ||
|
||
module FrederickAPI::V2::Errors | ||
describe Error do | ||
let(:result) { FrederickAPI::V2::Resource.new } | ||
let(:instance) { described_class.new(result) } | ||
let(:errors) { 'the errors' } | ||
|
||
before do | ||
allow(result).to receive(:errors).and_return errors | ||
end | ||
|
||
describe '#initialize' do | ||
context 'result has errors' do | ||
it 'sets env and errors' do | ||
expect(instance.errors).to be errors | ||
expect(instance.env).to be result | ||
end | ||
end | ||
|
||
context 'result does not have errors' do | ||
let(:errors) { nil } | ||
|
||
it 'sets env and errors' do | ||
expect(instance.errors).to eq [] | ||
expect(instance.env).to be result | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe BadRequest do | ||
subject { described_class.new(FrederickAPI::V2::Resource.new) } | ||
|
||
it { is_expected.to be_a(Error) } | ||
end | ||
|
||
describe UnprocessableEntity do | ||
subject { described_class.new(FrederickAPI::V2::Resource.new) } | ||
|
||
it { is_expected.to be_a(Error) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,17 +45,45 @@ | |
|
||
describe 'GET index' do | ||
context 'with no query params' do | ||
let(:resp) do | ||
{ | ||
status: 200, | ||
headers: { | ||
content_type: 'application/vnd.api+json' | ||
}, | ||
body: { | ||
data: [ | ||
{ | ||
'id': id, | ||
'type': 'users', | ||
'links': {}, | ||
'attributes': { | ||
'email': '[email protected]' | ||
}, | ||
'relationships': {} | ||
} | ||
] | ||
}.to_json | ||
} | ||
end | ||
|
||
before do | ||
stub_request(:get, base_url) | ||
.with(headers: request_headers) | ||
resource.with_access_token(access_token) { resource.all } | ||
.with(headers: request_headers).to_return(resp) | ||
end | ||
|
||
it 'makes GET request' do | ||
resource.with_access_token(access_token) { resource.all } | ||
expect( | ||
a_request(:get, base_url) | ||
).to have_been_made.once | ||
end | ||
|
||
it 'returns data' do | ||
expect( | ||
resource.with_access_token(access_token) { resource.all }.first | ||
).to be_a FrederickAPI::V2::User | ||
end | ||
end | ||
|
||
context 'with query params' do | ||
|
@@ -175,6 +203,72 @@ | |
end | ||
end | ||
end | ||
|
||
context 'bad request error' do | ||
let(:resp) do | ||
{ | ||
status: 400, | ||
headers: { | ||
content_type: 'application/vnd.api+json' | ||
}, | ||
body: { | ||
'errors' => [ | ||
{ | ||
'title' => 'property `not_a_real_contact_property` does not exist', | ||
'detail' => 'properties - property `not_a_real_contact_property` does not exist', | ||
'code' => '100', | ||
'source' => { 'pointer' => '/data/attributes/properties' }, | ||
'status' => '400' | ||
} | ||
] | ||
}.to_json | ||
} | ||
end | ||
|
||
before do | ||
stub_request(:get, base_url) | ||
.with(headers: request_headers).to_return(resp) | ||
end | ||
|
||
it 'raises' do | ||
expect do | ||
resource.with_access_token(access_token) { resource.all }.first | ||
end.to raise_error FrederickAPI::V2::Errors::BadRequest | ||
end | ||
end | ||
|
||
context 'unprocessable entity error' do | ||
let(:resp) do | ||
{ | ||
status: 422, | ||
headers: { | ||
content_type: 'application/vnd.api+json' | ||
}, | ||
body: { | ||
'errors' => [ | ||
{ | ||
'title' => 'property `not_a_real_contact_property` does not exist', | ||
'detail' => 'properties - property `not_a_real_contact_property` does not exist', | ||
'code' => '100', | ||
'source' => { 'pointer' => '/data/attributes/properties' }, | ||
'status' => '422' | ||
} | ||
] | ||
}.to_json | ||
} | ||
end | ||
|
||
before do | ||
stub_request(:get, base_url) | ||
.with(headers: request_headers).to_return(resp) | ||
end | ||
|
||
it 'raises' do | ||
expect do | ||
resource.with_access_token(access_token) { resource.all }.first | ||
end.to raise_error FrederickAPI::V2::Errors::UnprocessableEntity | ||
end | ||
end | ||
end | ||
|
||
describe 'PATCH' do | ||
|