Class: Utopia::Redirection::Errors

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

Direct Known Subclasses

FileNotFound

Instance Method Summary collapse

Constructor Details

#initialize(app, codes = {}) ⇒ Errors

Maps an error code to a given string



40
41
42
43
44
# File 'lib/utopia/redirection.rb', line 40

def initialize(app, codes = {})
	@codes = codes
	
	@app = app
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
# File 'lib/utopia/redirection.rb', line 52

def call(env)
	response = @app.call(env)
	
	if response[0] >= 400 and location = @codes[response[0]]
		error_request = env.merge(Rack::PATH_INFO => location, Rack::REQUEST_METHOD => Rack::GET)
		error_response = @app.call(error_request)

		if error_response[0] >= 400
			raise RequestFailure.new(env[Rack::PATH_INFO], response[0], location, error_response[0])
		else
			# Feed the error code back with the error document:
			error_response[0] = response[0]
			return error_response
		end
	else
		return response
	end
end

#freezeObject



46
47
48
49
50
# File 'lib/utopia/redirection.rb', line 46

def freeze
	@codes.freeze
	
	super
end