class Oktakit::Error

Custom error class for rescuing from all Okta errors

Public Class Methods

error(status) click to toggle source
# File lib/oktakit/error.rb, line 16
def self.error(status)
  case status
  when 400      then Oktakit::BadRequest
  when 401      then Oktakit::Unauthorized
  when 403      then Oktakit::Forbidden
  when 404      then Oktakit::NotFound
  when 405      then Oktakit::MethodNotAllowed
  when 406      then Oktakit::NotAcceptable
  when 409      then Oktakit::Conflict
  when 415      then Oktakit::UnsupportedMediaType
  when 422      then Oktakit::UnprocessableEntity
  when 400..499 then Oktakit::ClientError
  when 500      then Oktakit::InternalServerError
  when 501      then Oktakit::NotImplemented
  when 502      then Oktakit::BadGateway
  when 503      then Oktakit::ServiceUnavailable
  when 500..599 then Oktakit::ServerError
  end
end
from_response(response) click to toggle source

Returns the appropriate Oktakit::Error subclass based on status and response message

@param [Hash] response HTTP response @return [Oktakit::Error]

# File lib/oktakit/error.rb, line 9
def self.from_response(response)
  status = response[:status].to_i
  if klass = error(status)
    klass.new(response)
  end
end
new(response = nil) click to toggle source
Calls superclass method
# File lib/oktakit/error.rb, line 36
def initialize(response = nil)
  @response = response
  super(build_error_message)
end

Public Instance Methods

errors() click to toggle source

Array of validation errors @return [Array<Hash>] Error info

# File lib/oktakit/error.rb, line 43
def errors
  if data && data.is_a?(Hash)
    data[:errors] || []
  else
    []
  end
end

Private Instance Methods

build_error_message() click to toggle source
# File lib/oktakit/error.rb, line 80
def build_error_message
  return nil if @response.nil?

  message =  "#{@response[:method].to_s.upcase} "
  message << redact_url(@response[:url].to_s) + ': '
  message << "#{@response[:status]} - "
  message << response_message.to_s unless response_message.nil?
  message
end
data() click to toggle source
# File lib/oktakit/error.rb, line 53
def data
  @data ||= parse_data
end
parse_data() click to toggle source
# File lib/oktakit/error.rb, line 57
def parse_data
  body = @response[:body]
  return if body.empty?
  return body unless body.is_a?(String)

  headers = @response[:response_headers]
  content_type = headers && headers[:content_type] || ''
  if content_type =~ /json/
    Sawyer::Agent.serializer.decode(body)
  else
    body
  end
end
redact_url(url_string) click to toggle source
# File lib/oktakit/error.rb, line 90
def redact_url(url_string)
  %w[client_secret access_token].each do |token|
    url_string.gsub!(/#{token}=\S+/, "#{token}=(redacted)") if url_string.include? token
  end
  url_string
end
response_message() click to toggle source
# File lib/oktakit/error.rb, line 71
def response_message
  case data
  when Hash
    data[:errorSummary]
  when String
    data
  end
end