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.



11
12
13
14
15
# File 'lib/utopia/exceptions/handler.rb', line 11

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

Instance Method Details

#call(env) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/utopia/exceptions/handler.rb', line 41

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.



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

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



17
18
19
20
21
22
23
# File 'lib/utopia/exceptions/handler.rb', line 17

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

#log_exception(env, exception) ⇒ Object



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

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