Skip to content

Commit

Permalink
Merge pull request #55 from basecrm/error_without_content_type
Browse files Browse the repository at this point in the history
Error without content type
  • Loading branch information
Erwin Fedasz authored Jan 11, 2018
2 parents 9255091 + 4c4165f commit 767b13c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## CHANGELOG

v1.3.2 (2018-01-11)
**Features and Improvements**

* Fixed undefined method exception on error handling [issue #54](https://github.com/basecrm/basecrm-ruby/issues/54)

v1.3.1 (2017-12-08)
**Features and Improvements**

Expand Down Expand Up @@ -54,8 +59,8 @@ v1.2.1 (2016-11-18)

* Sync API support
* better handling of unexpected entity types
* `BaseCRM::SyncService#fetch` method returns `BaseCRM::Meta`instead of `BaseCRM::SyncMeta`.
This gives you additional information such as type.
* `BaseCRM::SyncService#fetch` method returns `BaseCRM::Meta`instead of `BaseCRM::SyncMeta`.
This gives you additional information such as type.
* High-level `BaseCRM::Sync` wrapper expects either `meta.sync.ack` or `meta.sync.nack` to be called.

### v1.1.0 (2015-05-29)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.1
1.3.2
10 changes: 6 additions & 4 deletions lib/basecrm/middlewares/raise_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ def on_complete(env)

content_type = env[:response_headers]['content-type']

unless content_type.include?('json')
raise "status=#{status} error=Unknown error occurred."
end

error_klass = case status
when 422 then BaseCRM::ResourceError
when 400...500 then BaseCRM::RequestError
when 500...600 then BaseCRM::ServerError
end

raise errors_collection(env, error_klass)
if !content_type.nil? && content_type.include?('json')
raise errors_collection(env, error_klass)
else
error = {:error=>{:code=>status, :message=>"Unknown error occurred."}, :meta=>{}}
raise error_klass.new(error)
end
end

def errors_collection(env, error_klass)
Expand Down
2 changes: 1 addition & 1 deletion lib/basecrm/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module BaseCRM
VERSION = "1.3.1"
VERSION = "1.3.2"
end
48 changes: 48 additions & 0 deletions spec/middlewares/raise_error_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "spec_helper"

describe BaseCRM::Middlewares::RaiseError do
before { @raise_error = BaseCRM::Middlewares::RaiseError.new }

describe :on_complete do

it "returns nil when code 200..300" do
expect { @raise_error.on_complete({:status => 204}) }.not_to raise_error
end

it "raises ResourceError when code 422 and no content type" do
expect { @raise_error.on_complete({:status => 422, :response_headers => {}}) }
.to raise_error do |error|
expect(error).to be_a(BaseCRM::ResourceError)
expect(error.code).to eql(422)
expect(error.message).to eql("Unknown error occurred.")
end
end

it "raises RequestError when code 400..500 and no content type" do
expect { @raise_error.on_complete({:status => 400, :response_headers => {}}) }
.to raise_error do |error|
expect(error).to be_a(BaseCRM::RequestError)
expect(error.code).to eql(400)
expect(error.message).to eql("Unknown error occurred.")
end
end

it "raises ServerError when code 500..600 and no content type" do
expect { @raise_error.on_complete({:status => 500, :response_headers => {}}) }
.to raise_error do |error|
expect(error).to be_a(BaseCRM::ServerError)
expect(error.code).to eql(500)
expect(error.message).to eql("Unknown error occurred.")
end
end

it "raises ResourceError when code 422 and XML (not supported) content type" do
expect { @raise_error.on_complete({:status => 422, :response_headers => {'content-type' => "application/xml"}}) }
.to raise_error do |error|
expect(error).to be_a(BaseCRM::ResourceError)
expect(error.code).to eql(422)
expect(error.message).to eql("Unknown error occurred.")
end
end
end
end

0 comments on commit 767b13c

Please sign in to comment.