Welcome to mirror list, hosted at ThFree Co, Russian Federation.

error_callback.rb « faraday « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f99be5b4d0481c78ecfda2fb32ae469d64a2212c (plain)
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
# frozen_string_literal: true

module Gitlab
  module Faraday
    # Simple Faraday Middleware that catches any error risen during the request and run the configured callback.
    # (https://lostisland.github.io/faraday/middleware/)
    #
    # By default, a no op callback is setup.
    #
    # Note that the error is not swallowed: it will be rerisen again. In that regard, this callback acts more
    # like an error spy than anything else.
    #
    # The callback has access to the request `env` and the exception instance. For more details, see
    # https://lostisland.github.io/faraday/middleware/custom
    #
    # Faraday.new do |conn|
    #   conn.request(
    #     :error_callback,
    #     callback: -> (env, exception) { Rails.logger.debug("Error #{exception.class.name} when trying to contact #{env[:url]}" ) }
    #   )
    #   conn.adapter(:net_http)
    # end
    class ErrorCallback < ::Faraday::Middleware
      def initialize(app, options = nil)
        super(app)
        @options = ::Gitlab::Faraday::ErrorCallback::Options.from(options) # rubocop: disable CodeReuse/ActiveRecord
      end

      def call(env)
        @app.call(env)
      rescue => e
        @options.callback&.call(env, e)

        raise
      end

      class Options < ::Faraday::Options.new(:callback)
        def callback
          self[:callback]
        end
      end
    end
  end
end