Sha256: f63a8015337748b163c9443917d06b40504532ad3001a5e1ae3c91f14aff22f2

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

module SlackBotManager
  class ConnectionClosed < StandardError; end
  class ConnectionRateLimited < StandardError; end
  class InvalidToken < StandardError; end
  class NoStorageMethod < StandardError; end
  class TokenAlreadyConnected < StandardError; end
  class TokenNotConnected < StandardError; end
  class TokenRevoked < StandardError; end

  module Errors
    # Mapping of error classes to type
    CLASS_ERROR_TYPES = {
      token_revoked: [
        SlackBotManager::InvalidToken,
        SlackBotManager::TokenRevoked
      ],
      rate_limited: [
        SlackBotManager::ConnectionRateLimited
      ],
      closed: [
        SlackBotManager::ConnectionClosed,
        Slack::RealTime::Client::ClientNotStartedError
      ]
    }.freeze

    # Regexp mapping of error keywords to type
    STRING_ERROR_TYPES = {
      token_revoked: /token_revoked|account_inactive|invalid_auth/i,
      rate_limited: /rate_limit|status 429/i,
      closed: /closed/i
    }.freeze

    def determine_error_type(err)
      # Check known error types, unless string
      CLASS_ERROR_TYPES.each { |k, v| return k if v.include?(err) } unless err.is_a?(String)

      # Check string matches for code responses or capture something inside it
      STRING_ERROR_TYPES.each { |k, v| return k if v.match(err.to_s) }

      :error
    end

    def on_error(err, _ = nil)
      error(err)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
slack-bot-manager-0.1.0pre5 lib/slack-bot-manager/errors.rb
slack-bot-manager-0.1.0pre4 lib/slack-bot-manager/errors.rb
slack-bot-manager-0.1.0pre3 lib/slack-bot-manager/errors.rb
slack-bot-manager-0.1.0pre2 lib/slack-bot-manager/errors.rb