-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
202 additions
and
13 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,30 @@ | ||
require 'webmachine/convert_request_to_rack_env' | ||
require 'pact_broker/configuration' | ||
|
||
module PactBroker | ||
module Api | ||
module Resources | ||
class ErrorHandler | ||
|
||
include PactBroker::Logging | ||
|
||
def self.call e, request, response | ||
logger.error e | ||
logger.error e.backtrace | ||
response.body = {:message => e.message, :backtrace => e.backtrace }.to_json | ||
report e, request | ||
end | ||
|
||
def self.report e, request | ||
PactBroker.configuration.api_error_reporters.each do | error_notifier | | ||
begin | ||
error_notifier.call(e, env: Webmachine::ConvertRequestToRackEnv.call(request)) | ||
rescue StandardError => e | ||
log_error(e, "Error executing api_error_reporter") | ||
end | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Webmachine | ||
class ConvertRequestToRackEnv | ||
def self.call(request) | ||
env = { | ||
'REQUEST_METHOD' => request.method.upcase, | ||
'CONTENT_TYPE' => request.headers['Content-Type'], | ||
'PATH_INFO' => request.uri.path, | ||
'QUERY_STRING' => request.uri.query || "", | ||
'SERVER_NAME' => request.uri.host, | ||
'SERVER_PORT' => request.uri.port.to_s, | ||
'SCRIPT_NAME' => '', | ||
'rack.url_scheme' => request.uri.scheme, | ||
'rack.input' => request.body.to_io ? StringIO.new(request.body.to_s) : nil | ||
} | ||
http_headers = request.headers.each do | key, value | | ||
env[convert_http_header_name_to_rack_header_name(key)] = value | ||
end | ||
env | ||
end | ||
|
||
def self.convert_http_header_name_to_rack_header_name(http_header_name) | ||
if http_header_name.downcase == 'content-type' || http_header_name.downcase == 'content-length' | ||
http_header_name.upcase.gsub('-', '_') | ||
else | ||
"HTTP_" + http_header_name.upcase.gsub('-', '_') | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require 'pact_broker/api/resources/error_handler' | ||
|
||
module PactBroker | ||
module Api | ||
module Resources | ||
describe ErrorHandler do | ||
describe "call" do | ||
let(:error) { PactBroker::Error.new('test error') } | ||
let(:thing) { double('thing', call: nil, another_call: nil) } | ||
let(:options) { { env: env } } | ||
let(:request) { double('request' ) } | ||
let(:response) { double('response', :body= => nil) } | ||
let(:env) { double('env') } | ||
|
||
subject { ErrorHandler.call(error, request, response) } | ||
|
||
before do | ||
allow(Webmachine::ConvertRequestToRackEnv).to receive(:call).and_return(env) | ||
PactBroker.configuration.add_api_error_reporter do | error, options | | ||
thing.call(error, options) | ||
end | ||
|
||
PactBroker.configuration.add_api_error_reporter do | error, options | | ||
thing.another_call(error, options) | ||
end | ||
end | ||
|
||
it "invokes the api error reporters" do | ||
expect(thing).to receive(:call).with(error, options) | ||
expect(thing).to receive(:another_call).with(error, options) | ||
subject | ||
end | ||
|
||
context "when the error reporter raises an error itself" do | ||
class TestError < StandardError; end | ||
|
||
before do | ||
expect(thing).to receive(:call).and_raise(TestError.new) | ||
end | ||
|
||
it "logs the error" do | ||
expect(PactBroker.logger).to receive(:error).at_least(1).times | ||
subject | ||
end | ||
|
||
it "does not propagate the error" do | ||
expect(thing).to receive(:another_call) | ||
subject | ||
end | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'webmachine/convert_request_to_rack_env' | ||
require 'webmachine/request' | ||
|
||
module Webmachine | ||
describe ConvertRequestToRackEnv do | ||
|
||
let(:rack_env) do | ||
{ | ||
"rack.input"=>StringIO.new('foo'), | ||
"REQUEST_METHOD"=>"POST", | ||
"SERVER_NAME"=>"example.org", | ||
"SERVER_PORT"=>"80", | ||
"QUERY_STRING"=>"", | ||
"PATH_INFO"=>"/foo", | ||
"rack.url_scheme"=>"http", | ||
"SCRIPT_NAME"=>"", | ||
"CONTENT_LENGTH"=>"0", | ||
"HTTP_HOST"=>"example.org", | ||
"CONTENT_TYPE"=>"application/x-www-form-urlencoded", | ||
} | ||
end | ||
|
||
let(:headers) do | ||
Webmachine::Headers.from_cgi(rack_env) | ||
end | ||
|
||
let(:rack_req) { ::Rack::Request.new(rack_env) } | ||
let(:webmachine_request) do | ||
Webmachine::Request.new(rack_req.request_method, | ||
rack_req.url, | ||
headers, | ||
Webmachine::Adapters::Rack::RequestBody.new(rack_req), | ||
nil, | ||
nil | ||
) | ||
end | ||
|
||
subject { ConvertRequestToRackEnv.call(webmachine_request) } | ||
|
||
describe ".call" do | ||
it "" do | ||
expected_env = rack_env.dup | ||
expected_env.delete('rack.input') | ||
actual_env = subject | ||
actual_rack_input = actual_env.delete('rack.input') | ||
expect(subject).to eq expected_env | ||
expect(actual_rack_input.string).to eq 'foo' | ||
end | ||
end | ||
end | ||
end |