Sha256: 6ab5d2514bcd3f0e735b9f37bcdc39c6eb9e9be9c08784db344fdb63a51f76f8

Contents?: true

Size: 1.48 KB

Versions: 34

Compression:

Stored size: 1.48 KB

Contents

module QuizApiClient
  module HttpRequest
    class Failure
      attr_reader :config

      def initialize(config)
        @config = config
      end

      def raise_error(
        method,
        url,
        response: nil,
        current_error: nil,
        error_class: QuizApiClient::HttpClient::RequestFailed
      )
        context = generate_error_context(method, url, response)

        record_error_context(context)

        raise error_class.new(context), error_message(current_error, url, response)
      end

      private

      def generate_error_context(method, url, response = nil)
        context = {
          quiz_api_client: {
            request: {
              method: method,
              url: url
            }
          }
        }

        if response
          context[:quiz_api_client][:response] = {
            body: response.body,
            code: response.code
          }
        end

        context
      end

      def record_error_context(context)
        case config.error_handler
        when :sentry_raven
          require 'sentry-raven'
          Raven.extra_context(context)
        end
      end

      def error_message(current_error, url, response)
        if current_error && response
          "#{current_error.message}: #{url} responded #{response.body} (#{response.code})"
        elsif current_error
          current_error.message
        elsif response
          "#{url} responded #{response.body} (#{response.code})"
        end
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
quiz_api_client-4.19.0 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.18.0 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.17.1 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.17.0 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.16.1 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.16.0 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.15.0 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.14.1 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.14.0 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.13.4 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.13.3 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.13.2 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.13.1 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.13.0 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.12.0 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.11.1 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.11.0 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.10.0 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.9.1 lib/quiz_api_client/http_request/failure.rb
quiz_api_client-4.9.0 lib/quiz_api_client/http_request/failure.rb