Module: Errlog::ControllerFilter

Defined in:
lib/errlog/rails_controller_extensions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/errlog/rails_controller_extensions.rb', line 4

def self.included base
  if Rails.env != 'test'
    base.send :prepend_before_filter, :errlog_connect_context
    base.send :rescue_from, Exception, :with => :errlog_report_exceptons
    base.send :helper_method, :errlog_context
    base.send :helper_method, :errlog_not_found
  end
end

Instance Method Details

#errlog_collect_context(ctx = nil) ⇒ Object



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
117
118
# File 'lib/errlog/rails_controller_extensions.rb', line 79

def errlog_collect_context ctx=nil
  ctx           ||= errlog_context
  ctx.component = "#{self.class.name}##{params[:action]}"
  ctx.params    = parametrize(params)
  headers       = {}
  request.headers.to_hash.each { |k, v|
    next if @@headers_exclusion_keys.any? { |s| k.starts_with?(s) }
    res = nil
    case v
      when Hash
        res = {}
        v.each { |k, v| res[k] = v.to_s }
      when Array;
        res = v.map &:to_s
      #when StringIO, IO
      #  next
      else
        res = v.to_s
    end
    headers[k.to_s] = res
  }
  ctx.headers = headers
  if respond_to?(:current_user)
    ctx.current_user = if current_user
                         res = [current_user.id]
                         res << current_user.email if current_user.respond_to? :email
                         res << current_user.full_name if current_user.respond_to? :full_name
                         res
                       else
                         "not logged in"
                       end
  end
  ctx.url         = request.url
  ctx.remote_ip   = request.remote_ip
  ctx.application = Errlog.application
  if ctx.application.blank? && request.url =~ %r|^https?://(.+?)[/:]|
    ctx.application = $1
  end
  ctx
end

#errlog_contextObject

Get the context that is linked to the current request and will pass all its data if reporting will be called. Dafault Loggerr.context might not always be connected to the request.



18
19
20
21
22
23
# File 'lib/errlog/rails_controller_extensions.rb', line 18

def errlog_context
  unless @errlog_context
    errlog_connect_context
  end
  @errlog_context
end

#errlog_not_found(text = 'Resource not found') ⇒ Object

Helper for 404’s or like. Can be used as the trap.

The argument may be either an Exception object or a text message. Can be used as the rescue_from catcher:

rescue_from :ActiveRecord::NotFound, :with => :errlog_not_found

or manually:

def load_item
  item = Item.find_by_id(params[:id]) or errlog_not_found

Reports and rethrows rails standard ActionController::RoutingError to activate default 404 processing



39
40
41
42
43
# File 'lib/errlog/rails_controller_extensions.rb', line 39

def errlog_not_found text = 'Resource not found'
  ex = ActionController::RoutingError.new text
  errlog_context.exception ex
  raise ex
end