UtopiaSourceUtopiaRedirectionErrors

class Errors

A middleware which performs internal redirects based on error status codes.

Definitions

def initialize(app, codes = {})

Implementation

def initialize(app, codes = {})
	@app = app
	@codes = codes
end

def freeze

Freeze this object and its internal state.

Signature

returns self

This object.

Implementation

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

def unhandled_error?(response)

Check whether the response status requires error handling.

Signature

parameter response Array

The response.

returns Boolean

Whether the response is an error without handler-provided headers.

Implementation

def unhandled_error?(response)
	response[0] >= 400 && response[1].empty?
end

def call(env)

Replace an unhandled error response with its configured error document.

Signature

parameter env Hash

The Rack environment.

returns Array

The original or error-document response.

raises RequestFailure

If the configured error document also fails.

Implementation

def call(env)
	response = @app.call(env)
	
	if unhandled_error?(response) && 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