Class: Hooks::App::CatchallEndpoint

Inherits:
Grape::API
  • Object
show all
Includes:
Helpers
Defined in:
lib/hooks/app/endpoints/catchall.rb

Class Method Summary collapse

Methods included from Helpers

#enforce_request_limits, #ip_filtering!, #load_handler, #parse_payload, #uuid

Class Method Details

.mount_path(config) ⇒ Object



27
28
29
30
31
# File 'lib/hooks/app/endpoints/catchall.rb', line 27

def self.mount_path(config)
  # :nocov:
  "#{config[:root_path]}/*path"
  # :nocov:
end

.route_block(captured_config, captured_logger) ⇒ Object



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)
  # :nocov:
  proc do
    request_id = uuid
    start_time = Time.now.utc

    # Use captured values
    config = captured_config
    log = captured_logger

    full_path = "#{config[:root_path]}/#{params[:path]}"

    handler_class_name = "DefaultHandler"
    http_method = "post"

    # Set request context for logging
    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,
          headers,
          request_context,
          config,
          start_time,
          full_path
        )
        rack_env = rack_env_builder.build

        # Enforce request limits
        enforce_request_limits(config)

        # Get raw body for payload parsing
        request.body.rewind
        raw_body = request.body.read

        # Parse payload
        payload = parse_payload(raw_body, headers)

        # Use default handler
        handler = DefaultHandler.new

        # Call handler
        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)

        # construct a standardized error response
        error_response = {
          error: "server_error",
          message: "an unexpected error occurred while processing the request",
          request_id:
        }

        # enrich the error response with details if not in production
        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
  # :nocov:
end