Sha256: 46bc2ab75f99e43df6422e0c45050a28ff32b7e008b9b04be57373a4d57a7b5c

Contents?: true

Size: 1.19 KB

Versions: 7

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

require 'calendly/error'

module Calendly
  # Calendly apis client error object.
  class ApiError < Error
    # @return [Faraday::Response]
    attr_reader :response
    # @return [Integer]
    attr_reader :status
    # @return [String]
    attr_reader :title
    # @return [OAuth2::Error, JSON::ParserError]
    attr_reader :cause_exception

    def initialize(response, cause_exception, message: nil)
      @response = response
      @cause_exception = cause_exception
      @message = message
      set_attributes_from_response
      @message ||= cause_exception.message if cause_exception
      super @message
    end

    def inspect
      "\#<#{self.class}:#{object_id} title:#{title}, status:#{status}>"
    end

  private

    def set_attributes_from_response # rubocop:disable Metrics/CyclomaticComplexity
      return unless response
      return unless response.respond_to? :body

      @status = response.status if response.respond_to? :status
      parsed = JSON.parse response.body, symbolize_names: true
      @title = parsed[:title] || parsed[:error]
      @message ||= parsed[:message] || parsed[:error_description]
    rescue JSON::ParserError
      nil
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
calendly-0.5.2 lib/calendly/api_error.rb
calendly-0.5.1 lib/calendly/api_error.rb
calendly-0.5.0 lib/calendly/api_error.rb
calendly-0.4.2 lib/calendly/api_error.rb
calendly-0.4.1 lib/calendly/api_error.rb
calendly-0.4.0 lib/calendly/api_error.rb
calendly-0.3.0 lib/calendly/api_error.rb