Sha256: 1ea793edc4035a644dc816fb774462ef1d53ae2f0dfe2e498cee2cdd110b068f

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require 'json'
require 'awesome_print'

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.1 lib/zumata_v3/errors.rb