Sha256: 9c7005dd11996ee3d1da439f8a8d86bd42d15f8fb2ec74dd32c5ef866db61a0d

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

# coding: utf-8
module Hallon
  # Thrown by Hallon on libspotify errors.
  #
  # @see http://developer.spotify.com/en/libspotify/docs/group__error.html
  class Error < RuntimeError
    class << self
      # Hash of error (Symbol) to code (Integer).
      #
      # @return [Hash<Symbol, Integer>]
      def table
        Spotify.enum_type(:error).to_hash
      end

      # Given a number or a symbol, find both the symbol and the error
      # number it represents.
      #
      # @param [Symbol, Fixnum] error
      # @return [[Fixnum, Symbol]] (error code, error symbol)
      def disambiguate(error)
        @enum ||= Spotify.enum_type(:error)

        if error.is_a? Symbol
          error = @enum[symbol = error]
        else
          symbol = @enum[error]
        end

        if error.nil? || symbol.nil?
          [-1, nil]
        else
          [error, symbol]
        end
      end

      # Explain a Spotify error with a string message.
      #
      # @param [Fixnum, Symbol]
      # @return [String]
      def explain(error)
        Spotify.error_message disambiguate(error)[0]
      end

      # Raise an {Error} with the given errno, unless it is `nil`, `:timeout`, `0` or `:ok`.
      #
      # @param [Fixnum, Symbol] error
      # @return [nil]
      def maybe_raise(x)
        return nil if [nil, :timeout].include?(x)

        error, symbol = disambiguate(x)
        return symbol if symbol == :ok

        message = []
        message << "[#{symbol.to_s.upcase}]"
        message << explain(error)
        message << "(#{error})"
        raise Hallon::Error, message.join(' ')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hallon-0.8.0 lib/hallon/error.rb