diff --git a/lib/rack/ecg.rb b/lib/rack/ecg.rb index cacc773..28632f4 100644 --- a/lib/rack/ecg.rb +++ b/lib/rack/ecg.rb @@ -11,6 +11,8 @@ class ECG DEFAULT_MOUNT_AT = "/_ecg" # Checks enabled by default. DEFAULT_CHECKS = [:http] + # Default failure response status. + DEFAULT_FAILURE_STATUS = 500 # Constructs an instance of ECG Rack middleware with the specified # options. @@ -22,7 +24,8 @@ class ECG # @param at [String, nil] Path which this ECG instance handles. # @param hook [#call, nil] Callable which receives the success status and # check results - def initialize(app = nil, checks: DEFAULT_CHECKS, at: DEFAULT_MOUNT_AT, hook: nil) + def initialize(app = nil, checks: DEFAULT_CHECKS, at: DEFAULT_MOUNT_AT, hook: nil, + failure_status: DEFAULT_FAILURE_STATUS) @app = app check_configuration = checks || [] @@ -30,6 +33,8 @@ def initialize(app = nil, checks: DEFAULT_CHECKS, at: DEFAULT_MOUNT_AT, hook: ni @mount_at = at || DEFAULT_MOUNT_AT @result_hook = hook + + @failure_response_status = failure_status end # Rack compatible call method. Not intended for direct usage. @@ -41,7 +46,7 @@ def call(env) success = check_results.none? { |check| check[1][:status] == Check::Status::ERROR } - response_status = success ? 200 : 500 + response_status = success ? 200 : @failure_response_status @result_hook&.call(success, check_results) diff --git a/spec/rack_middleware_spec.rb b/spec/rack_middleware_spec.rb index 2400fc4..f4052a6 100644 --- a/spec/rack_middleware_spec.rb +++ b/spec/rack_middleware_spec.rb @@ -67,10 +67,20 @@ let(:options) do { checks: [:error] } end - it "has a success error code" do + it "has a failure error code" do get "_ecg" expect(last_response.status).to(eq(500)) end + + context "with failure status option" do + let(:options) do + { checks: [:error], failure_status: 503 } + end + it "has a failure error code" do + get "_ecg" + expect(last_response.status).to(eq(503)) + end + end end context "when hook config option is set" do