-
-
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.
refactor: split up error logging, reporting and response generation
- Loading branch information
Showing
12 changed files
with
301 additions
and
220 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
41 changes: 41 additions & 0 deletions
41
lib/pact_broker/api/resources/error_response_body_generator.rb
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,41 @@ | ||
require 'pact_broker/configuration' | ||
|
||
module PactBroker | ||
module Api | ||
module Resources | ||
class ErrorResponseBodyGenerator | ||
include PactBroker::Logging | ||
|
||
# env not needed, just passing in in case PF ever needs it | ||
def self.call error, error_reference, env = {} | ||
response_body_hash(error, error_reference).to_json | ||
end | ||
|
||
def self.display_message(error, obfuscated_message) | ||
if PactBroker.configuration.show_backtrace_in_error_response? | ||
error.message || obfuscated_message | ||
else | ||
PactBroker::Errors.reportable_error?(error) ? obfuscated_message : error.message | ||
end | ||
end | ||
|
||
def self.obfuscated_error_message(error_reference) | ||
"An error has occurred. The details have been logged with the reference #{error_reference}" | ||
end | ||
|
||
def self.response_body_hash(error, error_reference) | ||
response_body = { | ||
error: { | ||
message: display_message(error, obfuscated_error_message(error_reference)), | ||
reference: error_reference | ||
} | ||
} | ||
if PactBroker.configuration.show_backtrace_in_error_response? | ||
response_body[:error][:backtrace] = error.backtrace | ||
end | ||
response_body | ||
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 |
---|---|---|
@@ -1,6 +1,4 @@ | ||
module PactBroker | ||
|
||
class Error < StandardError; end | ||
class TestError < StandardError; 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,28 @@ | ||
require 'pact_broker/configuration' | ||
require 'pact_broker/error' | ||
require 'pact_broker/logging' | ||
require 'securerandom' | ||
|
||
module PactBroker | ||
module Errors | ||
include PactBroker::Logging | ||
|
||
def self.generate_error_reference | ||
SecureRandom.urlsafe_base64.gsub(/[^a-z]/i, '')[0,10] | ||
end | ||
|
||
def self.reportable_error?(error) | ||
!error.is_a?(PactBroker::Error) && !error.is_a?(JSON::JSONError) | ||
end | ||
|
||
def self.report error, error_reference, request | ||
PactBroker.configuration.api_error_reporters.each do | error_notifier | | ||
begin | ||
error_notifier.call(error, env: request.env, error_reference: error_reference) | ||
rescue StandardError => e | ||
log_error(e, "Error executing api_error_reporter") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'pact_broker/configuration' | ||
require 'pact_broker/logging' | ||
|
||
module PactBroker | ||
module Errors | ||
class ErrorLogger | ||
include PactBroker::Logging | ||
|
||
# don't need the env, just in case PF needs it | ||
def self.call(error, error_reference, env = {}) | ||
if log_as_warning?(error) | ||
logger.warn("Error reference #{error_reference}", error) | ||
elsif PactBroker::Errors.reportable_error?(error) | ||
log_error(error, "Error reference #{error_reference}") | ||
else | ||
logger.info("Error reference #{error_reference}", error) | ||
end | ||
end | ||
|
||
def self.log_as_warning?(error) | ||
PactBroker.configuration.warning_error_classes.any? { |clazz| error.is_a?(clazz) || error.cause&.is_a?(clazz) } | ||
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
211 changes: 0 additions & 211 deletions
211
spec/lib/pact_broker/api/resources/error_handler_spec.rb
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.