From 7b0033e45dac5560cd416d4336123f17d4093172 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Wed, 22 Nov 2023 11:16:31 +1100 Subject: [PATCH] chore: load runtime error problem+json decorator from configuration object --- .../api/decorators/runtime_error_problem_json_decorator.rb | 2 +- lib/pact_broker/api/resources/error_response_generator.rb | 6 +++++- .../api/resources/error_response_generator_spec.rb | 6 ++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/pact_broker/api/decorators/runtime_error_problem_json_decorator.rb b/lib/pact_broker/api/decorators/runtime_error_problem_json_decorator.rb index a2f92f220..f43f76077 100644 --- a/lib/pact_broker/api/decorators/runtime_error_problem_json_decorator.rb +++ b/lib/pact_broker/api/decorators/runtime_error_problem_json_decorator.rb @@ -3,7 +3,7 @@ module PactBroker module Api module Decorators - class RuntimeErrorProblemJSONDecorator + class RuntimeErrorProblemJsonDecorator # @param message [String] def initialize(message) diff --git a/lib/pact_broker/api/resources/error_response_generator.rb b/lib/pact_broker/api/resources/error_response_generator.rb index d372c5d86..3a6e104cf 100644 --- a/lib/pact_broker/api/resources/error_response_generator.rb +++ b/lib/pact_broker/api/resources/error_response_generator.rb @@ -58,7 +58,7 @@ def self.display_message(error, message, obfuscated_message) end private_class_method def self.problem_json_response_body(message, env) - PactBroker::Api::Decorators::RuntimeErrorProblemJSONDecorator.new(message).to_hash(user_options: { base_url: env["pactbroker.base_url" ] }) + error_decorator_class(env).new(message).to_hash(user_options: { base_url: env["pactbroker.base_url" ] }) end private_class_method def self.obfuscated_error_message(error_reference) @@ -76,6 +76,10 @@ def self.display_message(error, message, obfuscated_message) private_class_method def self.problem_json?(env) env["HTTP_ACCEPT"]&.include?("application/problem+json") end + + private_class_method def self.error_decorator_class(env) + env["pactbroker.application_context"].decorator_configuration.class_for(:runtime_error_problem_json_decorator) + end end end end diff --git a/spec/lib/pact_broker/api/resources/error_response_generator_spec.rb b/spec/lib/pact_broker/api/resources/error_response_generator_spec.rb index 4f66b5537..b3e172ef3 100644 --- a/spec/lib/pact_broker/api/resources/error_response_generator_spec.rb +++ b/spec/lib/pact_broker/api/resources/error_response_generator_spec.rb @@ -1,4 +1,6 @@ require "pact_broker/api/resources/error_response_generator" +require "pact_broker/application_context" +require "pact_broker/api/decorators/runtime_error_problem_json_decorator" module PactBroker module Api @@ -13,7 +15,7 @@ module Resources let(:error_reference) { "bYWfnyWPlf" } let(:headers_and_body) { ErrorResponseGenerator.call(error, error_reference, rack_env) } - let(:rack_env) { { "pactbroker.base_url" => "http://example.org" } } + let(:rack_env) { { "pactbroker.base_url" => "http://example.org", "pactbroker.application_context" => PactBroker::ApplicationContext.default_application_context } } let(:headers) { headers_and_body.first } subject { JSON.parse(headers_and_body.last) } @@ -35,7 +37,7 @@ module Resources end context "when the Accept header includes application/problem+json" do - let(:rack_env) { { "HTTP_ACCEPT" => "application/hal+json, application/problem+json", "pactbroker.base_url" => "http://example.org" } } + let(:rack_env) { { "HTTP_ACCEPT" => "application/hal+json, application/problem+json", "pactbroker.base_url" => "http://example.org", "pactbroker.application_context" => PactBroker::ApplicationContext.default_application_context } } it "returns headers" do expect(headers).to eq("Content-Type" => "application/problem+json;charset=utf-8")