Sha256: 5dfa6d712755a3340d36feda5ca9c25d4315403f856089c9652ebfd344804a64

Contents?: true

Size: 1017 Bytes

Versions: 4

Compression:

Stored size: 1017 Bytes

Contents

# Copyright 2022 Daniel Herzog
# Use of this source code is governed by an MIT
# license that can be found in the LICENSE.md file.
# frozen_string_literal: true

module DeepL
  module Utils
    class ExceptionBuilder
      attr_reader :request, :response

      def self.error_class_from_response_code(code) # rubocop:disable Metrics/CyclomaticComplexity
        case code
        when 400 then Exceptions::BadRequest
        when 401, 403 then Exceptions::AuthorizationFailed
        when 404 then Exceptions::NotFound
        when 413 then Exceptions::RequestEntityTooLarge
        when 429 then Exceptions::LimitExceeded
        when 456 then Exceptions::QuotaExceeded
        when 500..599 then Exceptions::ServerError
        else Exceptions::RequestError
        end
      end

      def initialize(response)
        @response = response
      end

      def build
        error_class = self.class.error_class_from_response_code(response.code.to_i)
        error_class.new(response)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
deepl-rb-3.1.0 lib/deepl/utils/exception_builder.rb
deepl-rb-3.0.2 lib/deepl/utils/exception_builder.rb
deepl-rb-3.0.1 lib/deepl/utils/exception_builder.rb
deepl-rb-3.0.0 lib/deepl/utils/exception_builder.rb