Sha256: ebc2bda25518771530718bbced7da39d788b07c48d2e2abccd5ea3af9d29d07d

Contents?: true

Size: 1.97 KB

Versions: 5

Compression:

Stored size: 1.97 KB

Contents

require 'rack'

module JsonApiClient
  module Errors
    class ApiError < StandardError
      attr_reader :env

      def initialize(env, msg = nil)
        @env = env
        # Try to fetch json_api errors from response
        msg = track_json_api_errors(msg)

        super msg
      end

      private

      # Try to fetch json_api errors from response
      def track_json_api_errors(msg)
        return msg unless env.try(:body).kind_of?(Hash) || env.body.key?('errors')

        errors_msg = env.body['errors'].map { |e| e['title'] }.compact.join('; ').presence
        return msg unless errors_msg

        msg.nil? ? errors_msg : "#{msg} (#{errors_msg})"
        # Just to be sure that it is back compatible
      rescue StandardError
        msg
      end
    end

    class ClientError < ApiError
    end

    class ResourceImmutableError < StandardError
      def initialize(msg = 'Resource immutable')
        super msg
      end
    end

    class AccessDenied < ClientError
    end

    class NotAuthorized < ClientError
    end

    class ConnectionError < ApiError
    end

    class ServerError < ApiError
      def initialize(env, msg = nil)
        msg ||= begin
          status = env.status
          message = ::Rack::Utils::HTTP_STATUS_CODES[status]
          "#{status} #{message}"
        end

        super env, msg
      end
    end

    class Conflict < ServerError
      def initialize(env, msg = 'Resource already exists')
        super env, msg
      end
    end

    class NotFound < ServerError
      attr_reader :uri
      def initialize(uri)
        @uri = uri

        msg = "Couldn't find resource at: #{uri.to_s}"
        super nil, msg
      end
    end

    class InternalServerError < ServerError
    end

    class UnexpectedStatus < ServerError
      attr_reader :code, :uri
      def initialize(code, uri)
        @code = code
        @uri = uri

        msg = "Unexpected response status: #{code} from: #{uri.to_s}"
        super nil, msg
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
json_api_client-1.18.0 lib/json_api_client/errors.rb
json_api_client-1.17.1 lib/json_api_client/errors.rb
json_api_client-1.17.0 lib/json_api_client/errors.rb
json_api_client-1.16.1 lib/json_api_client/errors.rb
json_api_client-1.16.0 lib/json_api_client/errors.rb