33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/hooks/app/endpoints/catchall.rb', line 33
def self.route_block(captured_config, captured_logger)
proc do
request_id = uuid
start_time = Time.now.utc
config = captured_config
log = captured_logger
full_path = "#{config[:root_path]}/#{params[:path]}"
handler_class_name = "DefaultHandler"
http_method = "post"
request_context = {
request_id:,
path: full_path,
handler: handler_class_name
}
Hooks::Core::LogContext.with(request_context) do
begin
rack_env_builder = RackEnvBuilder.new(
request,
,
request_context,
config,
start_time,
full_path
)
rack_env = rack_env_builder.build
enforce_request_limits(config)
request.body.rewind
raw_body = request.body.read
payload = parse_payload(raw_body, )
handler = DefaultHandler.new
response = handler.call(
payload:,
headers:,
env: rack_env,
config: {}
)
log.info("successfully processed webhook event with handler: #{handler_class_name}")
log.debug("processing duration: #{Time.now.utc - start_time}s")
status 200
response
rescue StandardError => e
err_msg = "Error processing webhook event with handler: #{handler_class_name} - #{e.message} " \
"- request_id: #{request_id} - path: #{full_path} - method: #{http_method} - " \
"backtrace: #{e.backtrace.join("\n")}"
log.error(err_msg)
error_response = {
error: "server_error",
message: "an unexpected error occurred while processing the request",
request_id:
}
error_response[:backtrace] = e.backtrace.join("\n") unless @production
error_response[:message] = e.message unless @production
error_response[:handler] = handler_class_name unless @production
status determine_error_code(e)
error_response
end
end
end
end
|