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



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

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

Instance Method Details

#call(env) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/utopia/redirection.rb', line 51

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



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

def freeze
  @codes.freeze
  
  super
end