Sha256: 201634773d8e92c08968935acfa666574c9970426fced2961d3fa2dc4fb1fe5e

Contents?: true

Size: 1.38 KB

Versions: 5

Compression:

Stored size: 1.38 KB

Contents

module EtCcdClient
  module Exceptions
    class Base < ::StandardError
      attr_reader :original_exception, :url, :request

      def self.raise_exception(original_exception, **kw_args)
        expected_error_class = original_exception.class.name.split('::').last
        if EtCcdClient::Exceptions.const_defined?(expected_error_class)
          raise EtCcdClient::Exceptions.const_get(expected_error_class).new original_exception, **kw_args
        else
          raise new(original_exception, **kw_args)
        end
      end

      def self.exception(*args, **kw_args)
        new(*args, **kw_args)
      end

      def initialize(original_exception, url: nil, request: nil)
        self.original_exception = original_exception
        self.url = url
        self.request = request
      end

      def response
        original_exception.response
      end

      def to_s
        json = JSON.parse(response.body) rescue JSON::JSONError
        message = if json.nil? || json == JSON::JSONError
          ''
        else
          json['message'] || json['error'] || ''
        end
        if url
          "#{original_exception.message} - #{message} ('#{url}')"
        else
          "#{original_exception.message} - #{message}"
        end
      end

      private

      attr_writer :original_exception, :url

      def request=(request)
        @request = request&.args
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
et_ccd_client-0.4.0 lib/et_ccd_client/exceptions/base.rb
et_ccd_client-0.3.4 lib/et_ccd_client/exceptions/base.rb
et_ccd_client-0.3.3 lib/et_ccd_client/exceptions/base.rb
et_ccd_client-0.3.2 lib/et_ccd_client/exceptions/base.rb
et_ccd_client-0.3.1 lib/et_ccd_client/exceptions/base.rb