Sha256: f7e38afba5ae34bfc2687272f5b69e8feac79067b87644021e3bbd0cea12a29d

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module IronBank
  # Custom error class for rescuing from all Zuora API errors
  class Error < StandardError
    # Returns the appropriate IronBank::Error subclass based on status and
    # response message
    def self.from_response(response)
      status = response[:status].to_i

      klass = begin
        case status
        when 400      then IronBank::BadRequest
        when 404      then IronBank::NotFound
        when 422      then IronBank::UnprocessableEntity
        when 429      then IronBank::TooManyRequests
        when 500      then IronBank::InternalServerError
        when 400..499 then IronBank::ClientError
        when 500..599 then IronBank::ServerError
        end
      end

      return unless klass

      klass.new(response)
    end
  end

  # Raised on errors in the 400-499 range
  class ClientError < Error; end

  # Raised when Zuora returns a 400 HTTP status code
  class BadRequest < ClientError; end

  # Raised when Zuora returns a 404 HTTP status code
  class NotFound < ClientError; end

  # Raised when Zuora return 422 and unsuccessful action responses
  class UnprocessableEntity < Error; end

  # Raised when Zuora returns a 429 HTTP status code
  class TooManyRequests < ClientError; end

  # Raised on errors in the 500-599 range
  class ServerError < Error; end

  # Raised when Zuora returns a 500 HTTP status code
  class InternalServerError < ServerError; end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
iron_bank-1.0.3 lib/iron_bank/error.rb
iron_bank-1.0.2 lib/iron_bank/error.rb