File: helpful_socket_errors.rb

package info (click to toggle)
ruby-aws-sdk-core 3.104.3-3%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,444 kB
  • sloc: ruby: 11,201; makefile: 4
file content (43 lines) | stat: -rw-r--r-- 1,203 bytes parent folder | download | duplicates (3)
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
# frozen_string_literal: true

module Aws
  module Plugins
    # @api private
    class HelpfulSocketErrors < Seahorse::Client::Plugin

      class Handler < Seahorse::Client::Handler

        # Wrap `SocketError` errors with `Aws::Errors::NoSuchEndpointError`
        def call(context)
          response = @handler.call(context)
          response.context.http_response.on_error do |error|
            if socket_endpoint_error?(error)
              response.error = no_such_endpoint_error(context, error)
            end
          end
          response
        end

        private

        def socket_endpoint_error?(error)
          Seahorse::Client::NetworkingError === error &&
          SocketError === error.original_error &&
          error.original_error.message.match(/failed to open tcp connection/i) &&
          error.original_error.message.match(/getaddrinfo: nodename nor servname provided, or not known/i)
        end

        def no_such_endpoint_error(context, error)
          Errors::NoSuchEndpointError.new({
            context: context,
            original_error: error.original_error,
          })
        end

      end

      handle(Handler, step: :sign)

    end
  end
end