diff --git a/lib/recurly/errors/api_errors.rb b/lib/recurly/errors/api_errors.rb index cfd9f5567..343598c19 100644 --- a/lib/recurly/errors/api_errors.rb +++ b/lib/recurly/errors/api_errors.rb @@ -4,38 +4,80 @@ # need and we will usher them to the appropriate places. module Recurly module Errors - class BadRequestError < Errors::APIError; end + ERROR_MAP = { + "500" => "InternalServerError", + "502" => "BadGatewayError", + "503" => "ServiceUnavailableError", + "304" => "NotModifiedError", + "400" => "BadRequestError", + "401" => "UnauthorizedError", + "402" => "PaymentRequiredError", + "403" => "ForbiddenError", + "404" => "NotFoundError", + "406" => "NotAcceptableError", + "412" => "PreconditionFailedError", + "422" => "UnprocessableEntityError", + "429" => "TooManyRequestsError", + } - class InternalServerError < Errors::APIError; end + class ResponseError < Errors::APIError; end - class ImmutableSubscriptionError < Errors::APIError; end + class ServerError < ResponseError; end - class InvalidApiKeyError < Errors::APIError; end + class InternalServerError < ServerError; end - class InvalidApiVersionError < Errors::APIError; end + class BadGatewayError < ServerError; end - class InvalidContentTypeError < Errors::APIError; end + class ServiceUnavailableError < ServerError; end - class InvalidPermissionsError < Errors::APIError; end + class RedirectionError < ResponseError; end - class InvalidTokenError < Errors::APIError; end + class NotModifiedError < ResponseError; end - class NotFoundError < Errors::APIError; end + class ClientError < Errors::APIError; end - class SimultaneousRequestError < Errors::APIError; end + class BadRequestError < ClientError; end - class TransactionError < Errors::APIError; end + class InvalidContentTypeError < BadRequestError; end - class UnauthorizedError < Errors::APIError; end + class UnauthorizedError < ClientError; end - class UnavailableInApiVersionError < Errors::APIError; end + class PaymentRequiredError < ClientError; end - class UnknownApiVersionError < Errors::APIError; end + class ForbiddenError < ClientError; end - class ValidationError < Errors::APIError; end + class InvalidApiKeyError < ForbiddenError; end - class MissingFeatureError < Errors::APIError; end + class InvalidPermissionsError < ForbiddenError; end - class RateLimitedError < Errors::APIError; end + class NotFoundError < ClientError; end + + class NotAcceptableError < ClientError; end + + class UnknownApiVersionError < NotAcceptableError; end + + class UnavailableInApiVersionError < NotAcceptableError; end + + class InvalidApiVersionError < NotAcceptableError; end + + class PreconditionFailedError < ClientError; end + + class UnprocessableEntityError < ClientError; end + + class ValidationError < UnprocessableEntityError; end + + class MissingFeatureError < UnprocessableEntityError; end + + class TransactionError < UnprocessableEntityError; end + + class SimultaneousRequestError < UnprocessableEntityError; end + + class ImmutableSubscriptionError < UnprocessableEntityError; end + + class InvalidTokenError < UnprocessableEntityError; end + + class TooManyRequestsError < ClientError; end + + class RateLimitedError < TooManyRequestsError; end end end diff --git a/spec/recurly/errors_spec.rb b/spec/recurly/errors_spec.rb index 58909277f..b18e632a6 100644 --- a/spec/recurly/errors_spec.rb +++ b/spec/recurly/errors_spec.rb @@ -3,7 +3,7 @@ RSpec.describe Recurly::Errors::APIError do describe ".error_class" do - error_keys = Recurly::Errors.constants - [:APIError] + error_keys = Recurly::Errors.constants - [:APIError, :ERROR_MAP] error_keys = error_keys.map do |key| key.to_s.split(/(?=[A-Z])/).map(&:downcase).join("_") end