Sha256: a2b1072bea64b025c694561fb1a4f313e694af5775b9f156a8a142b523cce3db

Contents?: true

Size: 1.83 KB

Versions: 8

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module Discorb
  #
  # Error class for Discorb.
  # @abstract
  #
  class DiscorbError < StandardError
    private

    def enumerate_errors(hash)
      res = {}
      _recr_items([], hash, res)
      res
    end

    def _recr_items(key, item, res)
      case item
      when Array
        item.each_with_index do |v, i|
          _recr_items (key + [i]), v, res
        end
      when Hash
        item.each do |k, v|
          _recr_items (key + [k]), v, res
        end
      else
        res[key.join(".").gsub("_errors.", "")] = item
      end
    end
  end

  #
  # Represents a HTTP error.
  #
  class HTTPError < DiscorbError
    # @return [String] the HTTP response code.
    attr_reader :code
    # @return [Net::HTTPResponse] the HTTP response.
    attr_reader :response

    # @!visibility private
    def initialize(resp, data)
      @code = data[:code]
      @response = resp
      super(data[:message])
    end
  end

  #
  # Represents a 400 error.
  #
  class BadRequestError < HTTPError
    # @!visibility private
    def initialize(resp, data)
      @code = data[:code]
      @response = resp
      DiscorbError.instance_method(:initialize).bind(self).call(
        [data[:message], "\n", enumerate_errors(data[:errors]).map do |ek, ev|
          "#{ek}=>#{ev}"
        end.join("\n")].join("\n")
      )
    end
  end

  #
  # Represents a 403 error.
  #
  class ForbiddenError < HTTPError
  end

  #
  # Represents a 404 error.
  #
  class NotFoundError < HTTPError
  end

  #
  # Represents a error in client-side.
  #
  class ClientError < DiscorbError
  end

  #
  # Represents a timeout error.
  #
  class TimeoutError < DiscorbError
  end

  #
  # Represents a warning.
  #
  class NotSupportedWarning < DiscorbError
    def initialize(message)
      super("#{message} is not supported yet.")
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
discorb-0.0.8 lib/discorb/error.rb
discorb-0.0.7 lib/discorb/error.rb
discorb-0.0.6 lib/discorb/error.rb
discorb-0.0.5 lib/discorb/error.rb
discorb-0.0.4 lib/discorb/error.rb
discorb-0.0.3 lib/discorb/error.rb
discorb-0.0.2 lib/discorb/error.rb
discorb-0.0.1 lib/discorb/error.rb