Class: Utopia::Middleware::ExceptionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/middleware/redirector.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, location) ⇒ ExceptionHandler

Returns a new instance of ExceptionHandler.



27
28
29
30
31
# File 'lib/utopia/middleware/redirector.rb', line 27

def initialize(app, location)
	@app = app

	@location = location
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
	begin
		return @app.call(env)
	rescue Exception => ex
		log = ::Logger.new(env['rack.errors'])
		
		log.error "Exception #{ex.to_s.dump}!"
		
		ex.backtrace.each do |bt|
			log.error bt
		end
		
		if env['PATH_INFO'] == @location
			return fatal_error(env, ex)
		else

			# If redirection fails
			begin
				return redirect(env, ex)
			rescue
				return fatal_error(env, ex)
			end

		end
	end
end

#fatal_error(env, ex) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/utopia/middleware/redirector.rb', line 33

def fatal_error(env, ex)
	body = StringIO.new

	body.puts "<!DOCTYPE html><html><head><title>Fatal Error</title></head><body>"
	body.puts "<h1>Fatal Error</h1>"
	body.puts "<p>While requesting resource #{env['PATH_INFO'].to_html}, a fatal error occurred.</p>"
	body.puts "<blockquote><strong>#{ex.class.name.to_html}</strong>: #{ex.to_s.to_html}</blockquote>"
	body.puts "<p>There is nothing more we can do to fix the problem at this point.</p>"
	body.puts "<p>We apologize for the inconvenience.</p>"
	body.puts "</body></html>"
	body.rewind

	return [400, {"Content-Type" => "text/html"}, body]
end

#redirect(env, ex) ⇒ Object



48
49
50
# File 'lib/utopia/middleware/redirector.rb', line 48

def redirect(env, ex)
	return @app.call(env.merge('PATH_INFO' => @location, 'REQUEST_METHOD' => 'GET'))
end