Sha256: c3121ad7cef5a1bf8997d729459f789ac7552e8a0e569056bcc37505418df991

Contents?: true

Size: 1.86 KB

Versions: 96

Compression:

Stored size: 1.86 KB

Contents

require 'cgi'

module Aws
  module Xml
    class ErrorHandler < Seahorse::Client::Handler

      def call(context)
        @handler.call(context).on(300..599) do |response|
          response.error = error(context)
          response.data = nil
        end
      end

      private

      def error(context)
        body = context.http_response.body_contents
        if body.empty?
          code = http_status_error_code(context)
          message = ''
        else
          code, message = extract_error(body, context)
        end
        errors_module = context.client.class.errors_module
        errors_module.error_class(code).new(context, message)
      end

      def extract_error(body, context)
        [
          error_code(body, context),
          error_message(body),
        ]
      end

      def error_code(body, context)
        if matches = body.match(/<Code>(.+?)<\/Code>/)
          remove_prefix(unescape(matches[1]), context)
        else
          http_status_error_code(context)
        end
      end

      def http_status_error_code(context)
        status_code = context.http_response.status_code
        {
          302 => 'MovedTemporarily',
          304 => 'NotModified',
          400 => 'BadRequest',
          403 => 'Forbidden',
          404 => 'NotFound',
          412 => 'PreconditionFailed',
          413 => 'RequestEntityTooLarge',
        }[status_code] || "Http#{status_code}Error"
      end

      def remove_prefix(error_code, context)
        if prefix = context.config.api.metadata['errorPrefix']
          error_code.sub(/^#{prefix}/, '')
        else
          error_code
        end
      end

      def error_message(body)
        if matches = body.match(/<Message>(.+?)<\/Message>/m)
          unescape(matches[1])
        else
          ''
        end
      end

      def unescape(str)
        CGI.unescapeHTML(str)
      end

    end
  end
end

Version data entries

96 entries across 96 versions & 1 rubygems

Version Path
aws-sdk-core-3.52.1 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.52.0 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.51.0 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.50.0 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.49.0 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.48.6 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.48.5 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.48.4 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.48.3 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.48.2 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.48.1 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.48.0 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.47.0 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.46.2 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.46.1 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.46.0 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.45.0 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.44.2 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.44.1 lib/aws-sdk-core/xml/error_handler.rb
aws-sdk-core-3.44.0 lib/aws-sdk-core/xml/error_handler.rb