/
error_dumper.rb
132 lines (106 loc) · 3.75 KB
/
error_dumper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
70
71
72
73
74
75
76
77
78
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#
# Copyright (C) 2012 mkristian
#
# 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.
#
require 'ixtlan/errors/mailer'
require 'fileutils'
require 'slf4r/logger'
# unless Fixnum.respond_to? :days
# class Fixnum
# def days
# self
# end
# def ago
# DateTime.now - 86000 * self
# end
# end
# end
module Ixtlan
module Errors
class ErrorDumper
private
include ::Slf4r::Logger
public
attr_accessor :from_email, :to_emails, :keep_dumps, :base_url
def initialize
@keep_dumps = 30
end
def keep_dumps=(ttl)
old = @keep_dumps
@keep_dumps = ttl.to_i
daily_cleanup if old != @keep_dumps
end
def error_dump_model(model = nil)
@error_dump ||= model.nil? ? ::Error : model.classify
end
def dump(controller, exception)
daily_cleanup
error = dump_environment(exception, controller)
Mailer.error_notification(@from_email, @to_emails, exception, "#{@base_url}/#{error.id}").deliver unless (@to_emails.blank? || @from_email.blank?)
"#{@base_url}/#{error.id}"
end
private
def dump_environment(exception, controller)
dump = error_dump_model.new
dump.request = dump_hashmap(controller.request.env)
dump.response = dump_hashmap(controller.response.headers)
dump.session = dump_hashmap(controller.session)
dump.parameters = dump_hashmap(controller.params)
dump.message = exception.message
dump.clazz = exception.class.to_s
dump.backtrace = exception.backtrace.join("\n") if exception.backtrace
dump if dump.save
end
def dump_hashmap(map, indent = '')
result = ''
map.each do |key,value|
if value.is_a? Hash
result << "#{indent}#{key} => {\n"
result << dump_hashmap(value, "#{indent} ")
result << "#{indent}}\n"
else
result << "#{indent}#{key} => #{value.nil? ? 'nil': value}\n"
end
end
result
end
def daily_cleanup
if(@last_cleanup.nil? || @last_cleanup < 1.days.ago)
@last_cleanup = 0.days.ago # to have the right type
begin
delete_all
logger.info("cleaned error dumps")
rescue Exception => e
logger.error("cleanup error dumps", e)
end
end
end
private
if defined? ::DataMapper
def delete_all
error_dump_model.all(:created_at.lte => keep_dumps.days.ago).destroy!
end
else # ActiveRecord
def delete_all
error_dump_model.all(:conditions => ["created_at <= ?", keep_dumps.days.ago]).each(&:delete)
end
end
end
end
end