Skip to content

Commit

Permalink
Merge pull request #610 from recurly/better-errors
Browse files Browse the repository at this point in the history
Adding hierarchical errors
  • Loading branch information
bhelx authored Jul 27, 2020
2 parents 9746e09 + 9850fbf commit 40b1e13
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
76 changes: 59 additions & 17 deletions lib/recurly/errors/api_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/recurly/errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 40b1e13

Please sign in to comment.