Exception: LookerSDK::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/looker-sdk/error.rb

Direct Known Subclasses

ClientError, ServerError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error

Returns a new instance of Error.



60
61
62
63
# File 'lib/looker-sdk/error.rb', line 60

def initialize(response=nil)
  @response = response
  super(build_error_message)
end

Class Method Details

.error_for_401(headers) ⇒ Object

Returns most appropriate error for 401 HTTP status code



115
116
117
118
119
120
121
# File 'lib/looker-sdk/error.rb', line 115

def self.error_for_401(headers)
  if LookerSDK::OneTimePasswordRequired.required_header(headers)
    LookerSDK::OneTimePasswordRequired
  else
    LookerSDK::Unauthorized
  end
end

.error_for_403(body) ⇒ Object

Returns most appropriate error for 403 HTTP status code



125
126
127
128
129
130
131
132
133
# File 'lib/looker-sdk/error.rb', line 125

def self.error_for_403(body)
  if body =~ /rate limit exceeded/i
    LookerSDK::TooManyRequests
  elsif body =~ /login attempts exceeded/i
    LookerSDK::TooManyLoginAttempts
  else
    LookerSDK::Forbidden
  end
end

.from_response(response) ⇒ LookerSDK::Error

Returns the appropriate LookerSDK::Error sublcass based on status and response message

Parameters:

  • response (Hash)

    HTTP response

Returns:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/looker-sdk/error.rb', line 33

def self.from_response(response)
  status  = response[:status].to_i
  body    = response[:body].to_s
  headers = response[:response_headers]

  if klass =  case status
              when 400      then LookerSDK::BadRequest
              when 401      then error_for_401(headers)
              when 403      then error_for_403(body)
              when 404      then LookerSDK::NotFound
              when 405      then LookerSDK::MethodNotAllowed
              when 406      then LookerSDK::NotAcceptable
              when 409      then LookerSDK::Conflict
              when 415      then LookerSDK::UnsupportedMediaType
              when 422      then LookerSDK::UnprocessableEntity
              when 429      then LookerSDK::RateLimitExceeded
              when 400..499 then LookerSDK::ClientError
              when 500      then LookerSDK::InternalServerError
              when 501      then LookerSDK::NotImplemented
              when 502      then LookerSDK::BadGateway
              when 503      then LookerSDK::ServiceUnavailable
              when 500..599 then LookerSDK::ServerError
              end
    klass.new(response)
  end
end

Instance Method Details

#documentation_urlString

Documentation URL returned by the API for some errors

Returns:

  • (String)


68
69
70
# File 'lib/looker-sdk/error.rb', line 68

def documentation_url
  data[:documentation_url] if data.is_a? Hash
end

#error_doc_url(documentation_url) ⇒ String

Error Doc URL

Returns:

  • (String)


82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/looker-sdk/error.rb', line 82

def error_doc_url(documentation_url)
  return nil unless documentation_url
  regexp = Regexp.new("https://(?<redirector>docs\.looker\.com\|cloud\.google\.com/looker/docs)/r/err/(?<api_version>.*)/(?<status_code>\\d{3})(?<api_path>.*)", Regexp::IGNORECASE)
  match_data = regexp.match documentation_url
  return nil unless match_data

  key = "#{match_data[:status_code]}#{match_data[:api_path].gsub(/\/:([^\/]+)/,"/{\\1}")}"
  error_doc = error_docs[key] || error_docs[match_data[:status_code]]
  return nil unless error_doc

  return "https://marketplace-api.looker.com/errorcodes/#{error_doc[:url]}"
end

#error_docsObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/looker-sdk/error.rb', line 95

def error_docs
  @error_docs ||=
    begin
      sawyer_options = {
        :links_parser => Sawyer::LinkParsers::Simple.new,
        :serializer  => LookerSDK::Client::Serializer.new(JSON),
        :faraday => Faraday.new
      }

      agent = Sawyer::Agent.new("https://marketplace-api.looker.com", sawyer_options) do |http|
        http.headers[:accept] = 'application/json'
        #http.headers[:user_agent] = conn_hash[:user_agent]
      end
      response = agent.call(:get,"/errorcodes/index.json")
      response.data || []
    end
end

#errorsArray<Hash>

Array of validation errors

Returns:

  • (Array<Hash>)

    Error info



137
138
139
140
141
142
143
# File 'lib/looker-sdk/error.rb', line 137

def errors
  if data && data.is_a?(Hash)
    data[:errors] || []
  else
    []
  end
end

#messageString

Message string returned by the API for some errors

Returns:

  • (String)


75
76
77
# File 'lib/looker-sdk/error.rb', line 75

def message
  response_message
end