Sha256: 4da5a7d1f544a3085f09be742079910376967b4ecafe7b5a5ff8b19c8362af0b

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module RubyLokaliseApi
  # Standard API errors
  class Error < StandardError
    ClientError = Class.new(self)
    ServerError = Class.new(self)

    BadRequest = Class.new(ClientError)
    Unauthorized = Class.new(ClientError)
    NotAcceptable = Class.new(ClientError)
    NotFound = Class.new(ClientError)
    Conflict = Class.new(ClientError)
    TooManyRequests = Class.new(ClientError)
    Forbidden = Class.new(ClientError)
    Locked = Class.new(ClientError)
    MethodNotAllowed = Class.new(ClientError)

    NotImplemented = Class.new(ServerError)
    BadGateway = Class.new(ServerError)
    ServiceUnavailable = Class.new(ServerError)
    GatewayTimeout = Class.new(ServerError)

    ERRORS = {
      400 => RubyLokaliseApi::Error::BadRequest,
      401 => RubyLokaliseApi::Error::Unauthorized,
      403 => RubyLokaliseApi::Error::Forbidden,
      404 => RubyLokaliseApi::Error::NotFound,
      405 => RubyLokaliseApi::Error::MethodNotAllowed,
      406 => RubyLokaliseApi::Error::NotAcceptable,
      409 => RubyLokaliseApi::Error::Conflict,
      423 => RubyLokaliseApi::Error::Locked,
      429 => RubyLokaliseApi::Error::TooManyRequests,
      500 => RubyLokaliseApi::Error::ServerError,
      502 => RubyLokaliseApi::Error::BadGateway,
      503 => RubyLokaliseApi::Error::ServiceUnavailable,
      504 => RubyLokaliseApi::Error::GatewayTimeout
    }.freeze

    class << self
      # Create a new error from an HTTP response
      def from_response(body)
        msg = if body.respond_to?(:key)
                body.key?('error') ? body['error']['message'] : body['message']
              else
                body
              end

        new msg.to_s
      end
    end

    # Initializes a new Error object
    def initialize(message = '')
      super
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby-lokalise-api-9.3.0 lib/ruby_lokalise_api/error.rb
ruby-lokalise-api-9.2.1 lib/ruby_lokalise_api/error.rb
ruby-lokalise-api-9.2.0 lib/ruby_lokalise_api/error.rb