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

rescue_from.rb « gitlab_patches « active_record « lib « activerecord-gitlab « gems - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: eaaa90ec52b31c666ebcc1a18e0c6e29e857e404 (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
45
46
47
48
49
50
51
52
# frozen_string_literal: true

module ActiveRecord
  module GitlabPatches
    # This adds `rescue_from` to ActiveRecord::Base.
    # Currently only the following places will be handled by `rescue_from`:
    #
    # - `ActiveRecord::Relation#load`, and other methods that call
    #   `ActiveRecord::Relation#exec_queries`.
    # - `ActiveModel::UnknownAttributeError` as a result of `ActiveRecord::Base#assign_attributes`
    #
    # class ApplicationRecord < ActiveRecord::Base
    #   rescue_from MyException, with: :my_handler
    #
    #   def my_handler(exception)
    #     Rails.logger.info exception.message
    #
    #     raise exception
    #   end
    # end
    module RescueFrom
      extend ActiveSupport::Concern

      prepended do |base|
        base.include ActiveSupport::Rescuable
      end
    end

    module ExecQueriesRescueWithHandler
      def exec_queries
        super
      rescue StandardError => e
        # Method klass is defined in ActiveRecord gem lib/active_record/relation.rb
        klass.rescue_with_handler(e) || raise
      end
    end

    module AssignAttributesRescueWithHandler
      def _assign_attributes(...)
        super(...)
      rescue StandardError => e
        rescue_with_handler(e) || raise
      end
    end
  end
end

ActiveSupport.on_load(:active_record) do
  ActiveRecord::Relation.prepend(ActiveRecord::GitlabPatches::ExecQueriesRescueWithHandler)
  ActiveRecord::Base.prepend(ActiveRecord::GitlabPatches::AssignAttributesRescueWithHandler)
  ActiveRecord::Base.prepend(ActiveRecord::GitlabPatches::RescueFrom)
end