diff options
author | Robert Speicher <rspeicher@gmail.com> | 2021-01-20 22:34:23 +0300 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2021-01-20 22:34:23 +0300 |
commit | 6438df3a1e0fb944485cebf07976160184697d72 (patch) | |
tree | 00b09bfd170e77ae9391b1a2f5a93ef6839f2597 /lib/gitlab/faraday | |
parent | 42bcd54d971da7ef2854b896a7b34f4ef8601067 (diff) |
Add latest changes from gitlab-org/gitlab@13-8-stable-eev13.8.0-rc42
Diffstat (limited to 'lib/gitlab/faraday')
-rw-r--r-- | lib/gitlab/faraday/error_callback.rb | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/gitlab/faraday/error_callback.rb b/lib/gitlab/faraday/error_callback.rb new file mode 100644 index 00000000000..f99be5b4d04 --- /dev/null +++ b/lib/gitlab/faraday/error_callback.rb @@ -0,0 +1,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 |