Module: Webmachine
- Defined in:
- lib/webmachine/describe_routes.rb,
lib/pact_broker/api.rb,
lib/pact_broker/webmachine.rb,
lib/webmachine/render_error_monkey_patch.rb
Overview
Monkey patches the render_error method so that it returns hal+json or problem+json instead of text/html
Defined Under Namespace
Classes: Application, DescribeRoutes, Request
Class Method Summary collapse
- .build_rack_api(application_context) ⇒ Object
-
.decorator_configuration(req) ⇒ Object
rubocop: enable Metrics/ParameterLists.
-
.error_response_body(req, detail, title, type, status, request) ⇒ Object
rubocop: disable Metrics/ParameterLists.
- .error_response_content_type(request) ⇒ Object
- .original_render_error ⇒ Object
- .problem_json_error_content_type?(request) ⇒ Boolean
-
.render_error(code, req, res, options = {}) ⇒ Object
Renders a standard error message body for the response.
- .render_error_for_api(code, req, res, options) ⇒ Object
- .text_html_error_content_type?(request) ⇒ Boolean
Class Method Details
.build_rack_api(application_context) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/pact_broker/webmachine.rb', line 9 def self.build_rack_api(application_context) api = Webmachine::Application.new do |app| yield app end api.application_context = application_context api.configure do |config| config.adapter = :RackMapped end api.adapter end |
.decorator_configuration(req) ⇒ Object
rubocop: enable Metrics/ParameterLists
76 77 78 |
# File 'lib/webmachine/render_error_monkey_patch.rb', line 76 def self.decorator_configuration(req) req.env["pactbroker.application_context"].decorator_configuration end |
.error_response_body(req, detail, title, type, status, request) ⇒ Object
rubocop: disable Metrics/ParameterLists
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/webmachine/render_error_monkey_patch.rb', line 64 def self.error_response_body(req, detail, title, type, status, request) if problem_json_error_content_type?(request) decorator_configuration(req) .class_for(:custom_error_problem_json_decorator) .new(detail: detail, title: title, type: type, status: status) .to_json(user_options: { base_url: req.env["pactbroker.base_url"] }) else decorator_configuration(req).class_for(:error_decorator).new(detail).to_json end end |
.error_response_content_type(request) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/webmachine/render_error_monkey_patch.rb', line 53 def self.error_response_content_type(request) if problem_json_error_content_type?(request) "application/problem+json;charset=utf-8" elsif text_html_error_content_type?(request) "text/html;charset=utf-8" else "application/json;charset=utf-8" end end |
.original_render_error ⇒ Object
10 |
# File 'lib/webmachine/render_error_monkey_patch.rb', line 10 alias_method :original_render_error, :render_error |
.problem_json_error_content_type?(request) ⇒ Boolean
49 50 51 |
# File 'lib/webmachine/render_error_monkey_patch.rb', line 49 def self.problem_json_error_content_type?(request) request.headers["Accept"]&.include?("application/problem+json") end |
.render_error(code, req, res, options = {}) ⇒ Object
Renders a standard error message body for the response. The standard messages are defined in localization files.
20 21 22 23 24 25 26 |
# File 'lib/webmachine/render_error_monkey_patch.rb', line 20 def self.render_error(code, req, res, ={}) if text_html_error_content_type?(req) Webmachine.original_render_error(code, req, res, ) else render_error_for_api(code, req, res, ) end end |
.render_error_for_api(code, req, res, options) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/webmachine/render_error_monkey_patch.rb', line 28 def self.render_error_for_api(code, req, res, ) res.code = code unless res.body title, = t(["errors.#{code}.title", "errors.#{code}.message"], **{ :method => req.method, :error => res.error}.merge()) title = [:title] if [:title] = [:message] if [:message] res.body = error_response_body(req, , title, title.dasherize.gsub(/^\d+\-/, ""), code, req) res.headers[CONTENT_TYPE] = error_response_content_type(req) end ensure_content_length(res) ensure_date_header(res) end |
.text_html_error_content_type?(request) ⇒ Boolean
45 46 47 |
# File 'lib/webmachine/render_error_monkey_patch.rb', line 45 def self.text_html_error_content_type?(request) request.headers["Accept"]&.include?("text/html") end |