File: error_handler.rb

package info (click to toggle)
ruby-aws-sdk-core 3.212.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,232 kB
  • sloc: ruby: 17,533; makefile: 4
file content (100 lines) | stat: -rw-r--r-- 2,809 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# frozen_string_literal: true

require 'cgi'

module Aws
  module Xml
    class ErrorHandler < Aws::ErrorHandler

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

      private

      def extract_error(body, context)
        context[:request_id] = request_id(body)
        code = error_code(body, context)
        [
          code,
          error_message(body),
          error_data(context, body, code)
        ]
      end

      def error_data(context, body, code)
        data = EmptyStructure.new
        if (error_rules = context.operation.errors)
          error_rules.each do |rule|
            # query protocol may have custom error code
            # reference: https://smithy.io/2.0/aws/protocols/aws-query-protocol.html#error-code-resolution
            error_shape_code = rule.shape['error']['code'] if rule.shape['error']
            match = (code == error_shape_code || code == rule.shape.name)
            next unless match && rule.shape.members.any?

            data = parse_error_data(rule, body)
            # supporting HTTP bindings
            apply_error_headers(rule, context, data)
          end
        end
        data
      rescue Xml::Parser::ParsingError
        EmptyStructure.new
      end

      def parse_error_data(rule, body)
        # errors may nested under <Errors><Error>structure_data</Error></Errors>
        # Or may be flat and under <Error>structure_data</Error>
        body = body.tr("\n", '')
        if (matches = body.match(/<Error>(.+?)<\/Error>/))
          Parser.new(rule).parse("<#{rule.shape.name}>#{matches[1]}</#{rule.shape.name}>")
        else
          EmptyStructure.new
        end
      end

      def apply_error_headers(rule, context, data)
        headers = Aws::Rest::Response::Headers.new(rule)
        headers.apply(context.http_response, data)
      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 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 request_id(body)
        if (matches = body.match(/<RequestId>(.+?)<\/RequestId>/m))
          matches[1]
        end
      end

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

    end
  end
end