Skip to content

Commit

Permalink
move impl towards rack to allow cuba to use it
Browse files Browse the repository at this point in the history
  • Loading branch information
mkristian committed Dec 24, 2012
1 parent b6f719b commit 9302ae6
Show file tree
Hide file tree
Showing 13 changed files with 276 additions and 244 deletions.
1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -19,6 +19,7 @@ task :headers do
:copyright_holders => s.authors,
:copyright_years => [Time.now.year],
:add_path => 'lib',
:remove_path => 'lib',
:output_dir => './'
}

Expand Down
3 changes: 1 addition & 2 deletions ixtlan-error-handler.gemspec
@@ -1,5 +1,4 @@
# -*- mode: ruby -*-
IXTLAN_ERROR_HANDLER_DM_VERSION = '~> 1.2.0'
Gem::Specification.new do |s|
s.name = 'ixtlan-error-handler'
s.version = '0.2.1'
Expand All @@ -8,7 +7,7 @@ Gem::Specification.new do |s|
s.description = 'dump errors on filesystem and notify developers, map different errors to specific pages'
s.homepage = 'http://github.com/mkristian/ixtlan-error-handler'

s.authors = ['mkristian']
s.authors = ['Christian Meier']
s.email = ['m.kristian@web.de']

s.files = Dir['MIT-LICENSE']
Expand Down
20 changes: 0 additions & 20 deletions lib/ixtlan-error-handler.rb
@@ -1,23 +1,3 @@
#
# 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.
#
if defined?(Rails)
require 'ixtlan/errors/railtie'
end
17 changes: 17 additions & 0 deletions lib/ixtlan/errors/cuba.rb
@@ -0,0 +1,17 @@
require 'cuba_api'
require 'ixtlan/errors/resource'
require 'ixtlan/errors/serializer'
module Ixtlan
module Errors
class Cuba < ::CubaAPI
define do
on get, :number do |number|
write Ixtlan::Errors::Error.get!( number )
end
on get do
write Ixtlan::Errors::Error.all.reverse
end
end
end
end
end
126 changes: 126 additions & 0 deletions lib/ixtlan/errors/dumper.rb
@@ -0,0 +1,126 @@
require 'ixtlan/errors/mailer'
require 'fileutils'

module Ixtlan
module Errors
class Dumper

private

if defined? ::Slf4r
include ::Slf4r::Logger
else
require 'logger'
def logger
@logger ||= Logger.new( STDOUT )
end
end

public

attr_accessor :model, :block, :from_email, :to_emails, :keep_dumps, :base_url

def initialize( model = nil, &block )
@model = model
@keep_dumps = 30
block.call( self ) if block
@block = block
end

def model
@model ||= (Ixtlan::Errors::Error rescue nil)
end

def keep_dumps=(ttl)
old = @keep_dumps
@keep_dumps = ttl.to_i
daily_cleanup if old != @keep_dumps
end

def dump( exception, request, response, session , params )
update_config

daily_cleanup

error = dump_environment( exception,
request,
response,
session,
params )

if not @to_emails.blank? and not @from_email.blank?
Mailer.error_notification( @from_email,
@to_emails,
exception,
"#{@base_url}/#{error.id}" ).deliver
end
end

private

def update_config
block.call( self ) if block
end

def dump_environment( exception, request, response, session , params )
dump = model.new

dump.request = dump_hashmap( request )

dump.response = dump_hashmap( response )

dump.session = dump_hashmap( session )

dump.parameters = dump_hashmap( 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
return unless model
now = DateTime.now
if(@last_cleanup.nil? || @last_cleanup < (now - 1))
@last_cleanup = now
begin
delete_all( now - keep_logs )
logger.info "cleaned error dumps"
rescue Exception => e
logger.warn "error cleaning up error dumps: #{e.message}"
end
end
end

private

if defined? ::DataMapper
def delete_all( expired )
model.all( :created_at.lte => expired ).destroy!
end
else # ActiveRecord
def delete_all( expired )
model.all( :conditions => ["created_at <= ?", expired] ).each(&:delete)
end
end
end
end
end
132 changes: 0 additions & 132 deletions lib/ixtlan/errors/error_dumper.rb

This file was deleted.

26 changes: 5 additions & 21 deletions lib/ixtlan/errors/error_handler.rb
@@ -1,23 +1,3 @@
#
# 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.
#
module Ixtlan
module Errors
module ErrorHandler
Expand Down Expand Up @@ -81,7 +61,11 @@ def error_page(status, notice)
end

def dump_error(exception)
Rails.configuration.error_dumper.dump(self, exception)
Rails.configuration.error_dumper.dump( exception,
controller.request.env,
controller.response.headers,
controller.session,
controller.params )
end
end
end
Expand Down

0 comments on commit 9302ae6

Please sign in to comment.