Sha256: 9f2de6f7b8318c60e163a077e4671564d4ec0d5591cfbc161926b6f0559660b8

Contents?: true

Size: 1.34 KB

Versions: 20

Compression:

Stored size: 1.34 KB

Contents

require 'backlog_kit/response/middleware'

module BacklogKit
  class Response
    class RaiseError < BacklogKit::Response::Middleware
      CODE_ERRORS = {
        1  => BacklogKit::InternalError,
        2  => BacklogKit::LicenceError,
        3  => BacklogKit::LicenceExpiredError,
        4  => BacklogKit::AccessDeniedError,
        5  => BacklogKit::UnauthorizedOperationError,
        6  => BacklogKit::NoResourceError,
        7  => BacklogKit::InvalidRequestError,
        8  => BacklogKit::SpaceOverCapacityError,
        9  => BacklogKit::ResourceOverflowError,
        10 => BacklogKit::TooLargeFileError,
        11 => BacklogKit::AuthenticationError
      }.freeze

      def on_complete(env)
        super
        raise BacklogKit::Error, error_message unless success?
      end

      private

      def success?
        no_content? || !json? || json_body.is_a?(Array) || json_body['errors'].nil?
      end

      def error_message
        messages = []
        json_body['errors'].each.with_index(1) do |error, index|
          klass = CODE_ERRORS[error['code']] || BacklogKit::UnexpectedError
          messages.push(klass.build_error_message(error, index))
        end
        messages.join(', ')
      end

      def json_body
        @json_body ||= JSON.parse(body)
      end
    end

    Faraday::Response.register_middleware(error: RaiseError)
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
backlog_kit-0.19.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.18.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.17.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.16.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.15.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.14.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.13.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.12.1 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.12.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.11.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.10.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.9.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.8.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.7.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.6.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.5.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.4.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.3.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.2.0 lib/backlog_kit/response/raise_error.rb
backlog_kit-0.1.0 lib/backlog_kit/response/raise_error.rb