Sha256: 006c9d558332e52c0eb325948a5f7c3bc1b88f728239c6f9d9e021094aaf5cf0

Contents?: true

Size: 1.77 KB

Versions: 66

Compression:

Stored size: 1.77 KB

Contents

module Aws
  module Json
    class ErrorHandler < Xml::ErrorHandler

      # @param [Seahorse::Client::RequestContext] context
      # @return [Seahorse::Client::Response]
      def call(context)
        @handler.call(context).on(300..599) do |response|
          response.error = error(context)
          response.data = nil
        end
      end

      private

      def extract_error(body, context)
        json = Json.load(body)
        code = error_code(json, context)
        message = error_message(code, json)
        data = parse_error_data(context, code)
        [code, message, data]
      rescue Json::ParseError
        [http_status_error_code(context), '', EmptyStructure.new]
      end

      def error_code(json, context)
        code = json['__type']
        code ||= json['code']
        code ||= context.http_response.headers['x-amzn-errortype']
        if code
          code.split('#').last
        else
          http_status_error_code(context)
        end
      end

      def error_message(code, json)
        if code == 'RequestEntityTooLarge'
          'Request body must be less than 1 MB'
        else
          json['message'] || json['Message'] || ''
        end
      end

      def parse_error_data(context, code)
        data = EmptyStructure.new
        if error_rules = context.operation.errors
          error_rules.each do |rule|
            # match modeled shape name with the type(code) only
            # some type(code) might contains invalid characters
            # such as ':' (efs) etc
            match = rule.shape.name == code.gsub(/[^^a-zA-Z0-9]/, '')
            if match && rule.shape.members.any?
              data = Parser.new(rule).parse(context.http_response.body_contents)
            end
          end
        end
        data
      end

    end
  end
end

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
aws-sdk-core-3.100.0 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.99.2 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.99.1 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.99.0 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.98.0 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.97.1 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.97.0 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.96.1 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.96.0 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.95.0 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.94.1 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.94.0 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.93.0 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.92.0 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.91.1 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.91.0 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.90.1 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.90.0 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.89.1 lib/aws-sdk-core/json/error_handler.rb
aws-sdk-core-3.89.0 lib/aws-sdk-core/json/error_handler.rb