Class: Utopia::ExceptionHandler
- Inherits:
-
Object
- Object
- Utopia::ExceptionHandler
- Defined in:
- lib/utopia/exception_handler.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#fatal_error(env, exception) ⇒ Object
Generate a very simple fatal error response.
- #freeze ⇒ Object
-
#initialize(app, location) ⇒ ExceptionHandler
constructor
A new instance of ExceptionHandler.
- #log_exception(env, exception) ⇒ Object
- #redirect(env, exception) ⇒ Object
Constructor Details
#initialize(app, location) ⇒ ExceptionHandler
Returns a new instance of ExceptionHandler.
27 28 29 30 31 32 33 |
# File 'lib/utopia/exception_handler.rb', line 27 def initialize(app, location) @app = app @location = location self.freeze end |
Instance Method Details
#call(env) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/utopia/exception_handler.rb', line 84 def call(env) begin return @app.call(env) rescue Exception => exception log_exception(env, exception) # If the error occurred while accessing the error handler, we finish with a fatal error: if env[Rack::PATH_INFO] == @location return fatal_error(env, exception) else # If redirection fails, we also finish with a fatal error: begin return redirect(env, exception) rescue return fatal_error(env, exception) end end end end |
#fatal_error(env, exception) ⇒ Object
Generate a very simple fatal error response. This function should be unlikely to fail. Additionally, it generates a lowest common denominator response which should be suitable as a response to any kind of request. Ideally, this response is also not good or useful for any kind of higher level browser or API client, as this is not a normal error path but one that represents broken behaviour.
63 64 65 66 67 68 69 70 |
# File 'lib/utopia/exception_handler.rb', line 63 def fatal_error(env, exception) body = StringIO.new write_exception_to_stream(body, env, exception) body.rewind return [500, {HTTP::CONTENT_TYPE => "text/plain"}, body] end |
#freeze ⇒ Object
35 36 37 38 39 |
# File 'lib/utopia/exception_handler.rb', line 35 def freeze @location.freeze super end |
#log_exception(env, exception) ⇒ Object
72 73 74 75 76 |
# File 'lib/utopia/exception_handler.rb', line 72 def log_exception(env, exception) # An error has occurred, log it: output = env['rack.errors'] || $stderr write_exception_to_stream(output, env, exception, true) end |