Updated README.rdoc again
[feedcatcher.git] / vendor / rails / actionpack / lib / action_controller / failsafe.rb
1 module ActionController
2 class Failsafe
3 cattr_accessor :error_file_path
4 self.error_file_path = Rails.public_path if defined?(Rails.public_path)
5
6 def initialize(app)
7 @app = app
8 end
9
10 def call(env)
11 @app.call(env)
12 rescue Exception => exception
13 # Reraise exception in test environment
14 if env["rack.test"]
15 raise exception
16 else
17 failsafe_response(exception)
18 end
19 end
20
21 private
22 def failsafe_response(exception)
23 log_failsafe_exception(exception)
24 [500, {'Content-Type' => 'text/html'}, failsafe_response_body]
25 rescue Exception => failsafe_error # Logger or IO errors
26 $stderr.puts "Error during failsafe response: #{failsafe_error}"
27 end
28
29 def failsafe_response_body
30 error_path = "#{self.class.error_file_path}/500.html"
31 if File.exist?(error_path)
32 File.read(error_path)
33 else
34 "<html><body><h1>500 Internal Server Error</h1></body></html>"
35 end
36 end
37
38 def log_failsafe_exception(exception)
39 message = "/!\\ FAILSAFE /!\\ #{Time.now}\n Status: 500 Internal Server Error\n"
40 message << " #{exception}\n #{exception.backtrace.join("\n ")}" if exception
41 failsafe_logger.fatal(message)
42 end
43
44 def failsafe_logger
45 if defined?(Rails) && Rails.logger
46 Rails.logger
47 else
48 Logger.new($stderr)
49 end
50 end
51 end
52 end