Sha256: 526fda225dcc1b9a11b0b5f859846d89db5bf5d76fdf709e9595527654d97919

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require 'json'

module ZumataV3

  class ZumataError < StandardError; end
  class ClientConfigError < ZumataError; end

  class GeneralError < ZumataError; end
  class BadRequestError < ZumataError; end
  class InvalidApiKeyError < ZumataError; end
  class ForbiddenError < ZumataError; end
  class UnprocessableEntityError < ZumataError; end
  class NotFoundError < ZumataError; end
  class ServerError < ZumataError; end
  class SupplierError < ZumataError; end

  module ErrorClassifier
    def self.handle status_code, body
      json = nil
      begin
        json = JSON.parse(body)
      rescue JSON::ParserError
        raise GeneralError, body
      end
      classify_by_id(json)
    end

    def self.classify_by_id err
      case err["id"]
      when "bad_request"
        raise BadRequestError, err["message"]
      when "invalid_api_key"
        raise InvalidApiKeyError, err["message"]
      when "forbidden"
        raise ForbiddenError, err["message"]
      when "unprocessable_entity"
        raise UnprocessableEntityError, err["message"]
      when "not_found"
        raise NotFoundError, err["message"]
      when "server_error"
        raise ServerError, err["message"]
      when "supplier_error"
        raise SupplierError, err["message"]
      end
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zumata_v3-0.0.2 lib/zumata_v3/errors.rb