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



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

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)


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

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

#log_exception(exception) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rails_nexus.rb', line 115

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 = case deliverer
         when nil    then {}
         when Symbol then send(deliverer)
         when Proc   then deliverer.call(self)
         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.



104
105
106
107
# File 'lib/rails_nexus.rb', line 104

def log_exception_handler(exception)
  log_exception(exception) if exception.is_a?(StandardError)
  raise exception
end

#rescue_action(exception) ⇒ Object



109
110
111
112
113
# File 'lib/rails_nexus.rb', line 109

def rescue_action(exception)
  status = response_code_for_rescue(exception)
  log_exception(exception) if exception.is_a?(StandardError) && status != :not_found
  super
end