Class: Utopia::Exceptions::Handler

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

Overview

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.



25
26
27
28
29
# File 'lib/utopia/exceptions/handler.rb', line 25

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

Instance Method Details

#call(env) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/utopia/exceptions/handler.rb', line 53

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.



38
39
40
41
42
43
44
45
# File 'lib/utopia/exceptions/handler.rb', line 38

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



31
32
33
34
35
# File 'lib/utopia/exceptions/handler.rb', line 31

def freeze
	@location.freeze
	
	super
end

#log_exception(env, exception) ⇒ Object



47
48
49
50
51
# File 'lib/utopia/exceptions/handler.rb', line 47

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