Exception: Passwordstate::HTTPError

Inherits:
PasswordstateError show all
Defined in:
lib/passwordstate/errors.rb

Direct Known Subclasses

ClientError, ServerError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, request, response, errors = []) ⇒ HTTPError

Returns a new instance of HTTPError.



7
8
9
10
11
12
13
14
# File 'lib/passwordstate/errors.rb', line 7

def initialize(code, request, response, errors = [])
  @code = code.to_i
  @request = request
  @response = response
  @errors = errors

  super "Passwordstate responded with an error to the request:\n#{errors.map { |err| err['message'] || err['phrase'] }.join('; ')}"
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/passwordstate/errors.rb', line 5

def code
  @code
end

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/passwordstate/errors.rb', line 5

def errors
  @errors
end

#requestObject (readonly)

Returns the value of attribute request.



5
6
7
# File 'lib/passwordstate/errors.rb', line 5

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/passwordstate/errors.rb', line 5

def response
  @response
end

Class Method Details

.new_by_code(code, req, res, errors = []) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/passwordstate/errors.rb', line 16

def self.new_by_code(code, req, res, errors = [])
  code_i = code.to_i

  errtype = nil
  errtype ||= UnauthorizedError if code_i == 401
  errtype ||= ForbiddenError if code_i == 403
  errtype ||= NotFoundError if code_i == 404
  errtype ||= ClientError if code_i >= 400 && code_i < 500
  errtype ||= ServerError if code_i >= 500 && code_i < 600

  if code_i == 302 && res['location'].start_with?('/error/generalerror.aspx?')
    errtype ||= ServerError
    errors = [{ 'phrase' => 'Response code 302, most likely meaning an authorization error' }]
  end

  errtype ||= HTTPError
  errtype.new(code_i, req, res, errors)
end