Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNOE-137] Add Her middleware to handle errors #70

Merged
merged 1 commit into from
Aug 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions core/lib/her_extension/middleware/mnoe_raise_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Her
module Middleware
# This middleware will raise errors based on the response status
# Same as Faraday::Response::RaiseError, except it will catch specific
# errors codes rather than everything in 400..600
class MnoeRaiseError < Faraday::Response::RaiseError
def on_complete(env)
case env[:status]
when 407
# mimic the behavior that we get with proxy requests with HTTPS
raise Faraday::Error::ConnectionFailed,
%(407 "Proxy Authentication Required ")
when 502..504
raise Faraday::Error::ConnectionFailed, response_values(env)
when 401, 500
raise Faraday::Error::ClientError, response_values(env)
end
end
end
end
end
4 changes: 4 additions & 0 deletions core/lib/mno_enterprise/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require "her_extension/model/associations/association_proxy"
require "her_extension/model/associations/has_many_association"
require "her_extension/middleware/mnoe_api_v1_parse_json"
require "her_extension/middleware/mnoe_raise_error"
require "faraday_middleware"
require "mno_enterprise/engine"

Expand Down Expand Up @@ -266,6 +267,9 @@ def self.configure_api

# Adapter
c.use Faraday::Adapter::NetHttpNoProxy

# Error Handling
c.use Her::Middleware::MnoeRaiseError
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def api_stub_configure(api)

# Response
c.use Her::Middleware::MnoeApiV1ParseJson

c.use Her::Middleware::MnoeRaiseError

# Add stubs on the test adapter
c.use MnoeFaradayTestAdapter do |receiver|
@_stub_list.each do |key,stub|
Expand Down
22 changes: 22 additions & 0 deletions core/spec/models/mno_enterprise/base_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

module MnoEnterprise
RSpec.describe BaseResource, type: :model do
describe 'Error Handling' do
class Test < BaseResource; end

context 'Connection Errors' do
((502..504).to_a<<407).each do |code|
it "handles #{code} error" do
api_stub_for(get: "/tests/1", code: code)
expect { Test.find(1) }.to raise_error(Faraday::ConnectionFailed)
end
end
end

context 'Server Errors' do
[401, 500].each do |code|
it "handles #{code} error" do
api_stub_for(get: "/tests/1", code: code)
expect { Test.find(1) }.to raise_error(Faraday::Error::ClientError)
end
end
end
end

describe '#cache_key' do
context 'for existing record' do
let(:user) { build(:user) }
Expand Down