Skip to content

Commit 17f80ec

Browse files
committed
Implement error handling for simple text errors in BoomtownWithError handler and add corresponding acceptance test
1 parent 4587de4 commit 17f80ec

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

lib/hooks/app/api.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,24 +112,22 @@ def self.create(config:, endpoints:, log:)
112112
content_type "application/json"
113113
response.to_json
114114
rescue Hooks::Plugins::Handlers::Error => e
115-
# Handler called error! method - return the specified error response
116-
log.info("handler returned error response: #{handler_class_name} - status: #{e.status} - body: #{e.body}")
115+
# Handler called error! method - immediately return error response and exit the request
116+
log.debug("handler #{handler_class_name} called `error!` method")
117117

118-
# Call lifecycle hooks: on_response (treating error! as a valid response)
119-
if defined?(rack_env)
120-
Core::PluginLoader.lifecycle_plugins.each do |plugin|
121-
plugin.on_response(rack_env, e.body)
122-
end
123-
end
118+
error_response = nil
124119

125120
status e.status
126-
content_type "application/json"
127121
case e.body
128122
when String
129-
e.body
123+
content_type "text/plain"
124+
error_response = e.body
130125
else
131-
e.body.to_json
126+
content_type "application/json"
127+
error_response = e.body.to_json
132128
end
129+
130+
return error_response
133131
rescue StandardError => e
134132
err_msg = "Error processing webhook event with handler: #{handler_class_name} - #{e.message} " \
135133
"- request_id: #{request_id} - path: #{full_path} - method: #{http_method} - " \

spec/acceptance/acceptance_tests.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,17 @@ def expired_unix_timestamp(seconds_ago = 600)
508508
expect(body["foo"]).to eq("bar")
509509
expect(body["truthy"]).to eq(true)
510510
end
511+
512+
it "sends a POST request to the /webhooks/boomtown_with_error endpoint and it explodes with a simple text error" do
513+
payload = { boom_simple_text: true }.to_json
514+
response = make_request(:post, "/webhooks/boomtown_with_error", payload, json_headers)
515+
expect_response(response, Net::HTTPInternalServerError, "boomtown_with_error: the payload triggered a simple text boomtown error")
516+
517+
body = response.body
518+
expect(body).to eq("boomtown_with_error: the payload triggered a simple text boomtown error")
519+
expect(response.content_type).to eq("text/plain")
520+
expect(response.code).to eq("500")
521+
end
511522
end
512523
end
513524
end

spec/acceptance/plugins/handlers/boomtown_with_error.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ def call(payload:, headers:, env:, config:)
1818
}, 500)
1919
end
2020

21+
if payload["boom_simple_text"] == true
22+
log.error("boomtown simple text error triggered by payload: #{payload.inspect} - request_id: #{env["hooks.request_id"]}")
23+
24+
# Use Grape's `error!` method to return a simple text error response
25+
error!("boomtown_with_error: the payload triggered a simple text boomtown error", 500)
26+
end
27+
2128
return { status: "ok" }
2229
end
2330
end

0 commit comments

Comments
 (0)