root/lang/ruby/pritter/vendor/rails/actionpack/lib/action_controller/rescue.rb @ 5644

Revision 5644, 5.3 kB (checked in by nakamud, 6 years ago)

lang/ruby/pritter: freezed gems to ease installation

Line 
1module ActionController #:nodoc:
2  # Actions that fail to perform as expected throw exceptions. These exceptions can either be rescued for the public view
3  # (with a nice user-friendly explanation) or for the developers view (with tons of debugging information). The developers view
4  # is already implemented by the Action Controller, but the public view should be tailored to your specific application. So too
5  # could the decision on whether something is a public or a developer request.
6  #
7  # You can tailor the rescuing behavior and appearance by overwriting the following two stub methods.
8  module Rescue
9    def self.included(base) #:nodoc:
10      base.extend(ClassMethods)
11      base.class_eval do
12        alias_method_chain :perform_action, :rescue
13      end
14    end
15
16    module ClassMethods #:nodoc:
17      def process_with_exception(request, response, exception)
18        new.process(request, response, :rescue_action, exception)
19      end
20    end
21
22    protected
23      # Exception handler called when the performance of an action raises an exception.
24      def rescue_action(exception)
25        log_error(exception) if logger
26        erase_results if performed?
27
28        if consider_all_requests_local || local_request?
29          rescue_action_locally(exception)
30        else
31          rescue_action_in_public(exception)
32        end
33      end
34
35      # Overwrite to implement custom logging of errors. By default logs as fatal.
36      def log_error(exception) #:doc:
37        ActiveSupport::Deprecation.silence do
38          if ActionView::TemplateError === exception
39            logger.fatal(exception.to_s)
40          else
41            logger.fatal(
42              "\n\n#{exception.class} (#{exception.message}):\n    " +
43              clean_backtrace(exception).join("\n    ") +
44              "\n\n"
45            )
46          end
47        end
48      end
49
50      # Overwrite to implement public exception handling (for requests answering false to <tt>local_request?</tt>).
51      def rescue_action_in_public(exception) #:doc:
52        case exception
53          when RoutingError, UnknownAction
54            render_text(IO.read(File.join(RAILS_ROOT, 'public', '404.html')), "404 Not Found")
55          else
56            render_text(IO.read(File.join(RAILS_ROOT, 'public', '500.html')), "500 Internal Error")
57        end
58      end
59
60      # Overwrite to expand the meaning of a local request in order to show local rescues on other occurrences than
61      # the remote IP being 127.0.0.1. For example, this could include the IP of the developer machine when debugging
62      # remotely.
63      def local_request? #:doc:
64        [request.remote_addr, request.remote_ip] == ["127.0.0.1"] * 2
65      end
66
67      # Renders a detailed diagnostics screen on action exceptions.
68      def rescue_action_locally(exception)
69        add_variables_to_assigns
70        @template.instance_variable_set("@exception", exception)
71        @template.instance_variable_set("@rescues_path", File.dirname(rescues_path("stub")))   
72        @template.send(:assign_variables_from_controller)
73
74        @template.instance_variable_set("@contents", @template.render_file(template_path_for_local_rescue(exception), false))
75   
76        response.content_type = Mime::HTML
77        render_file(rescues_path("layout"), response_code_for_rescue(exception))
78      end
79   
80    private
81      def perform_action_with_rescue #:nodoc:
82        begin
83          perform_action_without_rescue
84        rescue Exception => exception  # errors from action performed
85          if defined?(Breakpoint) && params["BP-RETRY"]
86            msg = exception.backtrace.first
87            if md = /^(.+?):(\d+)(?::in `(.+)')?$/.match(msg) then
88              origin_file, origin_line = md[1], md[2].to_i
89
90              set_trace_func(lambda do |type, file, line, method, context, klass|
91                if file == origin_file and line == origin_line then
92                  set_trace_func(nil)
93                  params["BP-RETRY"] = false
94
95                  callstack = caller
96                  callstack.slice!(0) if callstack.first["rescue.rb"]
97                  file, line, method = *callstack.first.match(/^(.+?):(\d+)(?::in `(.*?)')?/).captures
98
99                  message = "Exception at #{file}:#{line}#{" in `#{method}'" if method}." # `� ( for ruby-mode)
100
101                  Breakpoint.handle_breakpoint(context, message, file, line)
102                end
103              end)
104
105              retry
106            end
107          end
108
109          rescue_action(exception)
110        end
111      end
112
113      def rescues_path(template_name)
114        File.dirname(__FILE__) + "/templates/rescues/#{template_name}.rhtml"
115      end
116
117      def template_path_for_local_rescue(exception)
118        rescues_path(
119          case exception
120            when MissingTemplate then "missing_template"
121            when RoutingError then "routing_error"
122            when UnknownAction   then "unknown_action"
123            when ActionView::TemplateError then "template_error"
124            else "diagnostics"
125          end
126        )
127      end
128     
129      def response_code_for_rescue(exception)
130        case exception
131          when UnknownAction, RoutingError
132            "404 Page Not Found"
133          else
134            "500 Internal Error"
135        end
136      end
137     
138      def clean_backtrace(exception)
139        exception.backtrace.collect { |line| Object.const_defined?(:RAILS_ROOT) ? line.gsub(RAILS_ROOT, "") : line }
140      end
141  end
142end
Note: See TracBrowser for help on using the browser.