Module: RailsNexus::ExceptionLoggable

Defined in:
lib/rails_nexus.rb

Overview

Copyright (c) 2005 Jamis Buck

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(target) ⇒ Object



86
87
88
89
90
91
# File 'lib/rails_nexus.rb', line 86

def self.included(target)
  target.extend(ClassMethods)
  target.class_attribute :local_addresses, :exception_data

  target.local_addresses = [IPAddr.new("127.0.0.1")]
end

Instance Method Details

#local_request? ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/rails_nexus.rb', line 99

def local_request?
  remote = IPAddr.new(request.remote_ip)
  !self.class.local_addresses.detect { |addr| addr.include?(remote) }.nil?
end

#log_exception(exception) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rails_nexus.rb', line 128

def log_exception(exception)
  return unless exception.is_a?(StandardError)

  # Support both the legacy class_attribute and the new configuration DSL
  deliverer = if self.class.exception_data
                self.class.exception_data
              elsif RailsNexus.configuration.exception_data
                RailsNexus.configuration.exception_data
              end

  data = begin
    case deliverer
    when nil    then {}
    when Symbol then send(deliverer)
    when Proc   then deliverer.call(self)
    else deliverer.respond_to?(:call) ? deliverer.call(self) : {}
    end
  rescue StandardError => data_error
    { exception_data_error: data_error.class.name }
  end

  data = RailsNexus.filter_sensitive_data(data || {})

  # Log with structured logger
  RailsNexus::Logger.error(exception, controller: self, extra: data)

  # Create the record
  LoggedException.create_from_exception(self, exception, data)
end

#log_exception_handler(exception) ⇒ Object

we log the exception and raise it again, for the normal handling.



105
106
107
108
109
110
111
112
113
114
# File 'lib/rails_nexus.rb', line 105

def log_exception_handler(exception)
  if exception.is_a?(StandardError)
    begin
      log_exception(exception)
    rescue StandardError => logging_error
      Rails.logger&.error("[RailsNexus] Failed to capture exception (#{logging_error.class})")
    end
  end
  raise exception
end

#rescue_action(exception) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rails_nexus.rb', line 116

def rescue_action(exception)
  status = response_code_for_rescue(exception)
  if exception.is_a?(StandardError) && status != :not_found
    begin
      log_exception(exception)
    rescue StandardError => logging_error
      Rails.logger&.error("[RailsNexus] Failed to capture exception (#{logging_error.class})")
    end
  end
  super
end