Class: Utopia::Exceptions::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/exceptions/handler.rb

Overview

A middleware which catches exceptions and performs an internal redirect.

Instance Method Summary collapse

Constructor Details

#initialize(app, location = '/errors/exception') ⇒ Handler

Returns a new instance of Handler.

Parameters:

  • location (String) (defaults to: '/errors/exception')

    Peform an internal redirect to this location when an exception is raised.



28
29
30
31
32
# File 'lib/utopia/exceptions/handler.rb', line 28

def initialize(app, location = '/errors/exception')
	@app = app
	
	@location = location
end

Instance Method Details

#call(env) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/utopia/exceptions/handler.rb', line 58

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
			begin
				# We do an internal redirection to the error location:
				error_request = env.merge(Rack::PATH_INFO => @location, Rack::REQUEST_METHOD => Rack::GET)
				error_response = @app.call(error_request)
				
				return [500, error_response[1], error_response[2]]
			rescue Exception
				# If redirection fails, we also finish with a fatal error:
				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.



43
44
45
46
47
48
49
50
# File 'lib/utopia/exceptions/handler.rb', line 43

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

#freezeObject



34
35
36
37
38
39
40
# File 'lib/utopia/exceptions/handler.rb', line 34

def freeze
	return self if frozen?
	
	@location.freeze
	
	super
end

#log_exception(env, exception) ⇒ Object



52
53
54
55
56
# File 'lib/utopia/exceptions/handler.rb', line 52

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