@@ -27,20 +27,36 @@ def self.route_block(captured_config, captured_logger)
2727 # :nocov:
2828 proc do
2929 request_id = uuid
30+ start_time = Time . now
3031
3132 # Use captured values
3233 config = captured_config
3334 log = captured_logger
3435
36+ full_path = "#{ config [ :root_path ] } /#{ params [ :path ] } "
37+
38+ handler_class_name = "DefaultHandler"
39+ http_method = "post"
40+
3541 # Set request context for logging
3642 request_context = {
3743 request_id :,
38- path : "/ #{ params [ :path ] } " ,
39- handler : "DefaultHandler"
44+ path : full_path ,
45+ handler : handler_class_name
4046 }
4147
4248 Hooks ::Core ::LogContext . with ( request_context ) do
4349 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+
4460 # Enforce request limits
4561 enforce_request_limits ( config )
4662
@@ -58,36 +74,34 @@ def self.route_block(captured_config, captured_logger)
5874 response = handler . call (
5975 payload :,
6076 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 ,
6578 config : { }
6679 )
6780
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" )
7183 status 200
7284 content_type "application/json"
73- ( response || { status : "ok" } ) . to_json
74-
85+ response . to_json
7586 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 )
7791
78- # Return error response
92+ # construct a standardized error response
7993 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 :
8397 }
8498
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 ]
89103
90- status error_response [ :code ]
104+ status determine_error_code ( e )
91105 content_type "application/json"
92106 error_response . to_json
93107 end
0 commit comments