Shopify / oktakit

Ruby toolkit for working with the Okta API
https://rubygems.org/gems/oktakit
MIT License
69 stars 60 forks source link

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

Open arharovets opened 2 years ago

arharovets commented 2 years ago

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('abrakadabrasdfskdkf@admin.com')

# response
#-> {:errorCode=>"E0000007", :errorSummary=> "Not found: Resource not found: abrakadabrasdfskdkf@admin.com (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: abrakadabrasdfskdkf@admin.com (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).