Skip to content

Commit b616780

Browse files
committed
consistent 500 server errors
1 parent 900161a commit b616780

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

lib/hooks/app/api.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,32 @@ def self.create(config:, endpoints:, log:)
123123
content_type "application/json"
124124
response.to_json
125125
rescue StandardError => e
126-
# Call lifecycle hooks: on_error
126+
err_msg = "Error processing webhook event with handler: #{handler_class_name} - #{e.message} " \
127+
"- request_id: #{request_id} - path: #{full_path} - method: #{http_method} - " \
128+
"backtrace: #{e.backtrace.join("\n")}"
129+
log.error(err_msg)
130+
131+
# call lifecycle hooks: on_error if the rack_env is available
132+
# if the rack_env is not available, it means the error occurred before we could build it
127133
if defined?(rack_env)
128134
Core::PluginLoader.lifecycle_plugins.each do |plugin|
129135
plugin.on_error(e, rack_env)
130136
end
131137
end
132138

133-
log.error("an error occuring during the processing of a webhook event - #{e.message}")
139+
# construct a standardized error response
134140
error_response = {
135-
error: e.message,
136-
code: determine_error_code(e),
141+
error: "server_error",
142+
message: "an unexpected error occurred while processing the request",
137143
request_id:
138144
}
139-
error_response[:backtrace] = e.backtrace unless config[:production]
140-
status error_response[:code]
145+
146+
# enrich the error response with details if not in production
147+
error_response[:backtrace] = e.backtrace.join("\n") unless config[:production]
148+
error_response[:message] = e.message unless config[:production]
149+
error_response[:handler] = handler_class_name unless config[:production]
150+
151+
status determine_error_code(e)
141152
content_type "application/json"
142153
error_response.to_json
143154
end

spec/acceptance/acceptance_tests.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,15 @@ def expired_unix_timestamp(seconds_ago = 600)
418418
headers = {}
419419
response = make_request(:post, "/webhooks/boomtown", payload, headers)
420420
expect_response(response, Net::HTTPInternalServerError, "Boomtown error occurred")
421+
body = parse_json_response(response)
422+
expect(body["error"]).to eq("server_error")
423+
expect(body["message"]).to eq("Boomtown error occurred")
424+
expect(body).to have_key("backtrace")
425+
expect(body["backtrace"]).to be_a(String)
426+
expect(body).to have_key("request_id")
427+
expect(body["request_id"]).to be_a(String)
428+
expect(body).to have_key("handler")
429+
expect(body["handler"]).to eq("Boomtown")
421430
end
422431
end
423432

0 commit comments

Comments
 (0)