Class: Lapis::Yggdrasil::Messaging::ErrorResponse

Inherits:
Response
  • Object
show all
Defined in:
lib/lapis/yggdrasil/messaging/error_response.rb

Overview

Response to a request indicating a problem occurred.

Instance Attribute Summary collapse

Attributes inherited from Response

#status

Instance Method Summary collapse

Methods inherited from Response

#ok?

Constructor Details

#initialize(message) ⇒ ErrorResponse

Creates a new error response by parsing a message from the server.

Parameters:

  • message (HTTP::Message)

    Error message from the server.



28
29
30
31
32
33
34
# File 'lib/lapis/yggdrasil/messaging/error_response.rb', line 28

def initialize(message)
  super(message)
  hash     = JSON.parse(message.body)
  @error   = hash['error']
  @message = hash['errorMessage']
  @cause   = hash['cause']
end

Instance Attribute Details

#causeString? (readonly)

Additional information about the root of the problem.

Returns:

  • (String)

    Additional information.

  • (nil)

    No additional information available.



24
25
26
# File 'lib/lapis/yggdrasil/messaging/error_response.rb', line 24

def cause
  @cause
end

#errorString (readonly)

Keywords describing the problem.

Returns:

  • (String)


15
16
17
# File 'lib/lapis/yggdrasil/messaging/error_response.rb', line 15

def error
  @error
end

#messageString (readonly)

Brief description of the problem.

Returns:

  • (String)


19
20
21
# File 'lib/lapis/yggdrasil/messaging/error_response.rb', line 19

def message
  @message
end

Instance Method Details

#to_errorAuthenticationError

Creates a throwable exception from the response information.

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lapis/yggdrasil/messaging/error_response.rb', line 38

def to_error
  code = case @error
           when 'Method Not Allowed'
             ErrorCodes::METHOD_NOT_ALLOWED
           when 'Not Found'
             ErrorCodes::NOT_FOUND
           when 'ForbiddenOperationException'
             if @cause == 'UserMigratedException'
               ErrorCodes::ACCOUNT_MIGRATED
             else
               case @message
                 when /username/
                   ErrorCodes::INVALID_CREDENTIALS
                 when /credentials/
                   ErrorCodes::TOO_MANY_ATTEMPTS
                 when /token/
                   ErrorCodes::INVALID_TOKEN
               end
             end
           when 'IllegalArgumentException'
             case @message
               when /profile/
                 ErrorCodes::PROFILE_NOT_SUPPORTED
               when /credentials/
                 ErrorCodes::NO_CREDENTIALS
             end
           when 'Unsupported Media Type'
             ErrorCodes::UNSUPPORTED_MEDIA_TYPE
         end
  AuthenticationError.new(code, @message)
end