Sha256: cad3248f170506711ca66dc8cc12c4a4a72ca9986828d8688cf9054fe528e337

Contents?: true

Size: 701 Bytes

Versions: 1

Compression:

Stored size: 701 Bytes

Contents

# frozen_string_literal: true

module Peddler
  class Error < StandardError
    class InvalidInput < Error; end
    class NotFound < Error; end
    class QuotaExceeded < Error; end
    class Unauthorized < Error; end

    attr_reader :cause

    # @!visibility private
    class << self
      def build(response)
        error = JSON.parse(response).dig("errors").first
        class_name = error.dig("code")
        klass = const_get(class_name)

        klass.new(error.dig("message"), response)
      rescue NameError
        const_set(class_name, Class.new(Error))
        retry
      end
    end

    def initialize(msg = nil, cause = nil)
      @cause = cause
      super(msg)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
peddler-3.0.0 lib/peddler/error.rb