Class: Airbrake::Rails::App Private

Inherits:
Object
  • Object
show all
Defined in:
lib/airbrake/rails/app.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

App is a wrapper around Rails.application.

Since:

  • v9.0.3

Defined Under Namespace

Classes: Route

Class Method Summary collapse

Class Method Details

.recognize_route(request) ⇒ Airbrake::Rails::App::Route?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/AbcSize

Parameters:

  • []

    request

Returns:

Since:

  • v9.0.3



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/airbrake/rails/app.rb', line 15

def self.recognize_route(request)
  # Duplicate `request` because `recognize` *can* strip the request's
  # `path_info`, which results in broken engine links (when the engine has
  # an isolated namespace).
  request_copy = request.dup

  # Save original script name because `router.recognize(request)` mutates
  # it. It's a Rails bug. More info in:
  #   * https://github.com/airbrake/airbrake/issues/1072
  #   * https://github.com/rails/rails/issues/31152
  original_script_name = request.env['SCRIPT_NAME']

  # We must search every engine individually to find a concrete route. If
  # we rely only on the `Rails.application.routes.router`, then the
  # recognize call would return the root route, neglecting PATH_INFO
  # completely. For example:
  #   * a request is made to `marketing#pricing`
  #   * `Rails.application` recognizes it as `marketing#/` (incorrect)
  #   * `Marketing::Engine` recognizes it as `marketing#/pricing` (correct)
  engines.each do |engine|
    engine.routes.router.recognize(request_copy) do |route, _params|
      # Restore original script name. Remove this code when/if the Rails
      # bug is fixed: https://github.com/airbrake/airbrake/issues/1072
      request.env['SCRIPT_NAME'] = original_script_name

      # Skip "catch-all" routes such as:
      #   get '*path => 'pages#about'
      #
      # Ideally, we should be using `route.glob?` but in Rails 7+ this
      # call would fail with a `NoMethodError`. This is because in
      # Rails 7+ the AST for the route is not kept in memory anymore.
      #
      # See: https://github.com/rails/rails/pull/43006#discussion_r783895766
      next if route.path.spec.any?(ActionDispatch::Journey::Nodes::Star)

      path =
        if engine == ::Rails.application
          route.path.spec.to_s
        else
          "#{engine.engine_name}##{route.path.spec}"
        end

      # Rails can recognize multiple routes for the given request. For
      # example, if we visit /users/2/edit, then Rails sees these routes:
      #   * "/users/:id/edit(.:format)"
      #   *  "/"
      #
      # We return the first route as, what it seems, the most optimal
      # approach.
      return Route.new(path)
    end
  end

  nil
end