Module: PactBroker::Api::Resources::ErrorHandlingMethods
- Included in:
- BaseResource
- Defined in:
- lib/pact_broker/api/resources/error_handling_methods.rb
Instance Method Summary collapse
-
#decorator_options_for_error ⇒ Object
If we use the normal decorator options that have policy objects we can get into recursive loops in Pactflow, so just make a simple variant of the decorator options here.
- #error_response_body(detail, title, type, status) ⇒ Object
- #error_response_content_type ⇒ Object
- #handle_exception(error) ⇒ Object
- #log_and_report_error(error) ⇒ Object
- #problem_json_error_content_type? ⇒ Boolean
- #set_json_error_message(detail, title: "Server error", type: "server-error", status: 500) ⇒ Object
- #set_json_validation_error_messages(errors) ⇒ Object
- #validation_errors_decorator_class(errors) ⇒ Object
- #validation_errors_response_body(errors) ⇒ Object
Instance Method Details
#decorator_options_for_error ⇒ Object
If we use the normal decorator options that have policy objects we can get into recursive loops in Pactflow, so just make a simple variant of the decorator options here
77 78 79 |
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 77 def { user_options: { base_url: base_url } } end |
#error_response_body(detail, title, type, status) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 53 def error_response_body(detail, title, type, status) if problem_json_error_content_type? decorator_class(:custom_error_problem_json_decorator).new(detail: detail, title: title, type: type, status: status).to_json(**) else decorator_class(:error_decorator).new(detail).to_json end end |
#error_response_content_type ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 45 def error_response_content_type if problem_json_error_content_type? "application/problem+json;charset=utf-8" else "application/hal+json;charset=utf-8" end end |
#handle_exception(error) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 11 def handle_exception(error) error_reference = log_and_report_error(error) headers, body = application_context.error_response_generator.call(error, error_reference, request.env) headers.each { | key, value | response.headers[key] = value } response.body = body end |
#log_and_report_error(error) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 19 def log_and_report_error(error) # generate reference error_reference = PactBroker::Errors.generate_error_reference # log error application_context.error_logger.call(error, error_reference, request.env) # report error application_context.error_reporter.call(error, error_reference, request.env) # generate response error_reference end |
#problem_json_error_content_type? ⇒ Boolean
71 72 73 |
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 71 def problem_json_error_content_type? request.headers["Accept"]&.include?("application/problem+json") end |
#set_json_error_message(detail, title: "Server error", type: "server-error", status: 500) ⇒ Object
34 35 36 37 |
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 34 def (detail, title: "Server error", type: "server-error", status: 500) response.headers["Content-Type"] = error_response_content_type response.body = error_response_body(detail, title, type, status) end |
#set_json_validation_error_messages(errors) ⇒ Object
40 41 42 43 |
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 40 def (errors) response.headers["Content-Type"] = error_response_content_type response.body = validation_errors_response_body(errors) end |
#validation_errors_decorator_class(errors) ⇒ Object
67 68 69 |
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 67 def validation_errors_decorator_class(errors) application_context.decorator_configuration.validation_error_decorator_class_for(errors.class, request.headers["Accept"]) end |
#validation_errors_response_body(errors) ⇒ Object
62 63 64 |
# File 'lib/pact_broker/api/resources/error_handling_methods.rb', line 62 def validation_errors_response_body(errors) validation_errors_decorator_class(errors).new(errors).to_json(**) end |