@@ -27,20 +27,36 @@ def self.route_block(captured_config, captured_logger)
27
27
# :nocov:
28
28
proc do
29
29
request_id = uuid
30
+ start_time = Time . now
30
31
31
32
# Use captured values
32
33
config = captured_config
33
34
log = captured_logger
34
35
36
+ full_path = "#{ config [ :root_path ] } /#{ params [ :path ] } "
37
+
38
+ handler_class_name = "DefaultHandler"
39
+ http_method = "post"
40
+
35
41
# Set request context for logging
36
42
request_context = {
37
43
request_id :,
38
- path : "/ #{ params [ :path ] } " ,
39
- handler : "DefaultHandler"
44
+ path : full_path ,
45
+ handler : handler_class_name
40
46
}
41
47
42
48
Hooks ::Core ::LogContext . with ( request_context ) do
43
49
begin
50
+ rack_env_builder = RackEnvBuilder . new (
51
+ request ,
52
+ headers ,
53
+ request_context ,
54
+ config ,
55
+ start_time ,
56
+ full_path
57
+ )
58
+ rack_env = rack_env_builder . build
59
+
44
60
# Enforce request limits
45
61
enforce_request_limits ( config )
46
62
@@ -58,36 +74,34 @@ def self.route_block(captured_config, captured_logger)
58
74
response = handler . call (
59
75
payload :,
60
76
headers :,
61
- env : { # a very limited Rack environment is present for catchall endpoints
62
- "REQUEST_METHOD" => request . request_method ,
63
- "hooks.request_id" => request_id ,
64
- } ,
77
+ env : rack_env ,
65
78
config : { }
66
79
)
67
80
68
- log . info "request processed successfully with default handler (id: #{ request_id } )"
69
-
70
- # Return response as JSON string when using txt format
81
+ log . info ( "successfully processed webhook event with handler: #{ handler_class_name } " )
82
+ log . debug ( "processing duration: #{ Time . now - start_time } s" )
71
83
status 200
72
84
content_type "application/json"
73
- ( response || { status : "ok" } ) . to_json
74
-
85
+ response . to_json
75
86
rescue StandardError => e
76
- log . error "request failed: #{ e . message } (id: #{ request_id } )"
87
+ err_msg = "Error processing webhook event with handler: #{ handler_class_name } - #{ e . message } " \
88
+ "- request_id: #{ request_id } - path: #{ full_path } - method: #{ http_method } - " \
89
+ "backtrace: #{ e . backtrace . join ( "\n " ) } "
90
+ log . error ( err_msg )
77
91
78
- # Return error response
92
+ # construct a standardized error response
79
93
error_response = {
80
- error : e . message ,
81
- code : determine_error_code ( e ) ,
82
- request_id : request_id
94
+ error : "server_error" ,
95
+ message : "an unexpected error occurred while processing the request" ,
96
+ request_id :
83
97
}
84
98
85
- # Add backtrace in all environments except production
86
- unless config [ :production ] == true
87
- error_response [ :backtrace ] = e . backtrace
88
- end
99
+ # enrich the error response with details if not in production
100
+ error_response [ :backtrace ] = e . backtrace . join ( " \n " ) unless config [ :production ]
101
+ error_response [ :message ] = e . message unless config [ :production ]
102
+ error_response [ :handler ] = handler_class_name unless config [ :production ]
89
103
90
- status error_response [ :code ]
104
+ status determine_error_code ( e )
91
105
content_type "application/json"
92
106
error_response . to_json
93
107
end
0 commit comments