Skip to content

raise Oktakit::Error.from_response(response) fails with TypeError #59

@arharovets

Description

@arharovets

Hi there,

I've encountered an unexpected behavior when trying to raise Oktakit::Error.from_response(response) causing TypeError - exception object expected with oktakit version 0.3.1.

Steps to reproduce

okta_client = Oktakit.new(token: token, api_endpoint: api_endpoint)
response, http_status = okta_client.get_user('[email protected]')

# response
#-> {:errorCode=>"E0000007", :errorSummary=> "Not found: Resource not found: [email protected] (User)", :errorLink=>"E0000007", :errorId=>"oaeLRic8zbhTBiJ81eJnWTQUg", :errorCauses=>[]}
# response.class
#-> Sawyer::Resource

raise Oktakit::Error.from_response(response) unless http_status == 200
#->TypeError - exception object expected:
#-> ... trace ...
###
response = {:errorCode=>"E0000007", :errorSummary=> "Not found: Resource not found: [email protected] (User)", :errorLink=>"E0000007", :errorId=>"oaeLRic8zbhTBiJ81eJnWTQUg", :errorCauses=>[]}
###

# lib/oktakit/error.rb
module Oktakit
  # Custom error class for rescuing from all Okta errors
  class Error < StandardError
    # Returns the appropriate Oktakit::Error subclass based
    # on status and response message
    #
    # @param [Hash] response HTTP response
    # @return [Oktakit::Error]
    def self.from_response(response)
      status = response[:status].to_i # nil.to_i = 0
      if (klass = error(status)) # this block returns nil
        klass.new(response)
      end
    end
    ...
    
    def build_error_message
      return nil if @response.nil?

      message =  "#{@response[:method].to_s.upcase} " # no corresponding attribute
      message << redact_url(@response[:url].to_s) + ': ' # same here
      message << "#{@response[:status]} - " # and same here
      message << response_message.to_s unless response_message.nil?
      message
    end
    ...

It looks like the structure of the response object for errors has changed (documentation link).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions