Permalink
Browse files

added specs

  • Loading branch information...
1 parent f6bbdda commit 8e002306f07c8d8120fdc3cee6282cd508e1057c @mkristian committed Mar 22, 2011
View
1 Mavenfile
@@ -0,0 +1 @@
+properties["jruby.plugins.version"] = "0.26.0-SNAPSHOT"
View
8 ixtlan-error-handler.gemspec
@@ -18,14 +18,8 @@ Gem::Specification.new do |s|
s.rdoc_options = ['--main','README.textile']
s.files += Dir['lib/**/*']
s.files += Dir['spec/**/*']
- s.files += Dir['features/**/*rb']
- s.files += Dir['features/**/*feature']
s.test_files += Dir['spec/**/*_spec.rb']
- s.test_files += Dir['features/*.feature']
- s.test_files += Dir['features/step_definitions/*.rb']
- s.add_development_dependency 'rails', '3.0.1'
- s.add_development_dependency 'rspec', '2.0.1'
- s.add_development_dependency 'cucumber', '0.9.4'
+ s.add_development_dependency 'rspec', '2.4.0'
s.add_development_dependency 'rake', '0.8.7'
end
View
2 ixtlan-error-handler.gemspec.files
@@ -0,0 +1,2 @@
+ixtlan-error-handler.gemspec
+Mavenfile
View
4 lib/ixtlan/errors/error_dumper.rb
@@ -1,4 +1,6 @@
require 'ixtlan/errors/mailer'
+require 'fileutils'
+
module Ixtlan
module Errors
class ErrorDumper
@@ -65,7 +67,7 @@ def dump_environment(logger, exception, controller)
dump_environment_header(logger, "EXCEPTION");
logger.error("#{exception.class}:#{exception.message}")
- logger.error("\t" + exception.backtrace.join("\n\t"))
+ logger.error("\t" + exception.backtrace.join("\n\t")) if exception.backtrace
end
def dump_hashmap(logger, map)
View
37 lib/ixtlan/errors/error_handler.rb
@@ -5,28 +5,32 @@ module ErrorHandler
protected
def internal_server_error(exception)
+ log_user_error(exception)
dump_error(exception)
- status = :internal_server_error
- error_page(:internal_server_error, exception, "internal server error: #{exception.class.name}")
+ error_page(:internal_server_error, "internal server error: #{exception.class.name}")
end
def page_not_found(exception)
log_user_error(exception)
- status = rescue_responses[exception.class.name]
- status = status == :internal_server_error ? :not_found : status
- error_page(status, exception, "page not found")
+ error_page(:not_found, "page not found")
end
+ #status = rescue_responses[exception.class.name]
+ #status = status == :internal_server_error ? :not_found : status
+ #error_page(status, "page not found")
def stale_resource(exception)
log_user_error(exception)
- respond_to do |format|
- format.html {
- render_stale_error_page
- }
- format.xml { head :conflict }
- end
+ error_page(:conflict, "stale resource")
end
+ # respond_to do |format|
+ # format.html {
+ # render_stale_error_page
+ # }
+ # format.xml { head :conflict }
+ # end
+ # end
+
def render_error_page_with_session(status)
render :template => "errors/error_with_session", :status => status
end
@@ -35,15 +39,15 @@ def render_error_page(status)
render :template => "errors/error", :status => status
end
- def render_stale_error_page
- render :template => "errors/stale", :status => :conflict
- end
+ # def render_stale_error_page
+ # render :template => "errors/stale", :status => :conflict
+ # end
private
if defined? ::Ixtlan::Audit
def user_logger
- @user_logger ||= Ixtlan::Audit::UserLogger.new(Rails.application.config.audit_manager)
+ @user_logger ||= Ixtlan::Audit::UserLogger.new(Rails.configuration.audit_manager)
end
def log_user_error(exception)
@@ -56,7 +60,7 @@ def log_user_error(exception)
end
end
- def error_page(status, exception, notice)
+ def error_page(status, notice)
respond_to do |format|
format.html {
@notice = notice
@@ -72,7 +76,6 @@ def error_page(status, exception, notice)
end
def dump_error(exception)
- log_user_error(exception)
Rails.configuration.error_dumper.dump(self, exception)
end
end
View
113 spec/error_dumper_spec.rb
@@ -0,0 +1,113 @@
+module Ixtlan
+ module Errors
+ module ActionMailer
+ class Base
+
+ def self.method_missing(method, *args)
+ self.new
+ end
+
+ def self.deliver(val = nul)
+ @delivered = val if val
+ @delivered
+ end
+ def deliver
+ self.class.deliver(true)
+ end
+ end
+ end
+ end
+end
+
+require 'ixtlan/errors/error_dumper'
+require 'date'
+require 'logger'
+
+class Controller
+
+ def request
+ self
+ end
+ def env
+ @env ||= {:one => 1, :two => 2, :three => 3}
+ end
+
+ def response
+ self
+ end
+
+ def headers
+ @headers ||= { 'ONE' => 1, "TWO" => 2 }
+ end
+
+ def session
+ @session ||= { "user" => "me" }
+ end
+
+ def params
+ @params ||= { :query => "all" }
+ end
+end
+
+class NilClass
+ def blank?
+ true
+ end
+end
+
+class String
+ def blank?
+ size == 0
+ end
+end
+
+class Fixnum
+ def ago
+ DateTime.now - self.to_i
+ end
+end
+
+class DateTime
+ def tv_sec
+ sec
+ end
+ def tv_usec
+ 0
+ end
+end
+
+describe Ixtlan::Errors::ErrorDumper do
+
+ before :each do
+ @dumper = Ixtlan::Errors::ErrorDumper.new
+ @dumper.dump_dir = "target"
+ @controller = Controller.new
+ end
+
+ it "should dump env and not send notification" do
+ file = @dumper.dump(@controller, StandardError.new("dump it"))
+ File.exists?(file).should be_true
+ @dumper.email_from = "asd"
+ file = @dumper.dump(@controller, StandardError.new("dump it"))
+ File.exists?(file).should be_true
+ @dumper.email_from = nil
+ @dumper.email_to = "dsa"
+ file = @dumper.dump(@controller, StandardError.new("dump it"))
+ File.exists?(file).should be_true
+ end
+
+ it "should clean up error dumps" do
+ @dumper.keep_dumps = 0
+ @dumper.dump(@controller, StandardError.new("dump it"))
+ Dir['target/error-*'].size.should == 1
+ @dumper.dump(@controller, StandardError.new("dump it"))
+ Dir['target/error-*'].size.should == 1
+ end
+
+ it "should send notifications" do
+ @dumper.email_to = "das"
+ @dumper.email_from = "asd"
+ @dumper.dump(@controller, StandardError.new("dump it"))
+ Ixtlan::Errors::ActionMailer::Base.delivered.should be_true
+ end
+end
View
138 spec/error_handler_spec.rb
@@ -0,0 +1,138 @@
+require 'ixtlan/errors/error_handler'
+#require 'date'
+require 'logger'
+
+class Controller
+ def logger
+ @logger ||= Logger.new(STDOUT)
+ end
+
+ def respond_to(&block)
+ block.call self
+ end
+
+ def html(&block)
+ block.call self
+ end
+
+ def xml(&block)
+ block.call self
+ end
+
+ def json(&block)
+ block.call self
+ end
+
+ def stati
+ @stati ||= []
+ end
+
+ def head(status)
+ stati << status
+ end
+
+ def render(options = nil)
+ @render = options if options
+ @render
+ end
+end
+
+class Rails
+ def self.configuration
+ self
+ end
+
+ def self.error_dumper
+ self
+ end
+
+ def self.dump(controller, exception)
+ @dumped = exception
+ end
+
+ def self.dumped
+ r = @dumped
+ @dumped = nil
+ r
+ end
+end
+
+describe Ixtlan::Errors::ErrorHandler do
+
+ before :each do
+ @controller = Controller.new
+ Controller.send :include, Ixtlan::Errors::ErrorHandler
+ end
+
+ describe "without session" do
+
+ it "should handle internal server error" do
+ exp = StandardError.new("internal")
+ @controller.send :internal_server_error, exp
+ Rails.dumped.should == exp
+ @controller.render.should_not be_nil
+ @controller.render[:status].should == :internal_server_error
+ @controller.render[:template].should == "errors/error"
+ @controller.stati.should == [:internal_server_error, :internal_server_error]
+ end
+
+ it "should handle not found" do
+ exp = StandardError.new("not found")
+ @controller.send :page_not_found, exp
+ Rails.dumped.should be_nil
+ @controller.render.should_not be_nil
+ @controller.render[:status].should == :not_found
+ @controller.render[:template].should == "errors/error"
+ @controller.stati.should == [:not_found, :not_found]
+ end
+
+ it "should handle stale resource" do
+ exp = StandardError.new("stale")
+ @controller.send :stale_resource, exp
+ Rails.dumped.should be_nil
+ @controller.render.should_not be_nil
+ @controller.render[:status].should == :conflict
+ @controller.render[:template].should == "errors/error"
+ @controller.stati.should == [:conflict, :conflict]
+ end
+ end
+
+ describe "with session" do
+
+ before :each do
+ def @controller.current_user
+ Object.new
+ end
+ end
+
+ it "should handle internal server error" do
+ exp = StandardError.new("internal")
+ @controller.send :internal_server_error, exp
+ Rails.dumped.should == exp
+ @controller.render.should_not be_nil
+ @controller.render[:status].should == :internal_server_error
+ @controller.render[:template].should == "errors/error_with_session"
+ @controller.stati.should == [:internal_server_error, :internal_server_error]
+ end
+
+ it "should handle not found" do
+ exp = StandardError.new("not found")
+ @controller.send :page_not_found, exp
+ Rails.dumped.should be_nil
+ @controller.render.should_not be_nil
+ @controller.render[:status].should == :not_found
+ @controller.render[:template].should == "errors/error_with_session"
+ @controller.stati.should == [:not_found, :not_found]
+ end
+
+ it "should handle stale resource" do
+ exp = StandardError.new("stale")
+ @controller.send :stale_resource, exp
+ Rails.dumped.should be_nil
+ @controller.render.should_not be_nil
+ @controller.render[:status].should == :conflict
+ @controller.render[:template].should == "errors/error_with_session"
+ @controller.stati.should == [:conflict, :conflict]
+ end
+ end
+end

0 comments on commit 8e00230

Please sign in to comment.