Module: Faultline::ExceptionLoggable

Defined in:
lib/faultline.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



60
61
62
63
64
65
# File 'lib/faultline.rb', line 60

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

#filter_sub_parameters(params, rails_filter_parameters) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/faultline.rb', line 90

def filter_sub_parameters(params, rails_filter_parameters)
  params.each do |key, value|
    if(value.class != Hash and value.class != ActiveSupport::HashWithIndifferentAccess)
      params[key] = '[FILTERED]' if rails_filter_parameters.include?(key.to_sym)
    else
      params[key] = filter_sub_parameters(value, rails_filter_parameters)
    end
  end
  params
end

#local_request?Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/faultline.rb', line 73

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

#log_exception(exception) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/faultline.rb', line 101

def log_exception(exception)
  # Support both the legacy class_attribute and the new configuration DSL
  deliverer = if self.class.exception_data
                self.class.exception_data
              elsif Faultline.configuration.exception_data
                Faultline.configuration.exception_data
              end

  data = case deliverer
         when nil    then {}
         when Symbol then send(deliverer)
         when Proc   then deliverer.call(self)
         end

  rails_filter_parameters = defined?(::Rails) ? ::Rails.application.config.filter_parameters : []

  self.request.parameters.each do |key, value|
    if(value.class != Hash and value.class != ActiveSupport::HashWithIndifferentAccess)
      self.request.parameters[key] = '[FILTERED]' if rails_filter_parameters.include?(key.to_sym)
    else
      self.request.parameters[key] = filter_sub_parameters(value, rails_filter_parameters)
    end
  end if rails_filter_parameters.any?

  # Log with structured logger
  Faultline::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.



79
80
81
82
# File 'lib/faultline.rb', line 79

def log_exception_handler(exception)
  log_exception(exception)
  raise exception
end

#rescue_action(exception) ⇒ Object



84
85
86
87
88
# File 'lib/faultline.rb', line 84

def rescue_action(exception)
  status = response_code_for_rescue(exception)
  log_exception(exception) if status != :not_found
  super
end