Permalink
Browse files

move impl towards rack to allow cuba to use it

  • Loading branch information...
mkristian committed Dec 24, 2012
1 parent b6f719b commit 9302ae6d67b62c3cf931cca84e5fe932cb6826d2
View
@@ -19,6 +19,7 @@ task :headers do
:copyright_holders => s.authors,
:copyright_years => [Time.now.year],
:add_path => 'lib',
+ :remove_path => 'lib',
:output_dir => './'
}
@@ -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'
@@ -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']
@@ -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
View
@@ -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
View
@@ -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
@@ -1,132 +0,0 @@
-#
-# 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
@@ -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
@@ -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
Oops, something went wrong.

0 comments on commit 9302ae6

Please sign in to comment.