Class: Sq::Dbsync::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/sq/dbsync/error_handler.rb

Overview

Handles redacting sensitive information for error messages, and delegating response to a user-defined handler.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ErrorHandler

Returns a new instance of ErrorHandler.



7
8
9
10
# File 'lib/sq/dbsync/error_handler.rb', line 7

def initialize(config)
  @config = config
  @handler = config.fetch(:error_handler, ->(ex) {})
end

Instance Method Details

#notify_error(tag, ex) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/sq/dbsync/error_handler.rb', line 22

def notify_error(tag, ex)
  with_massaged_exception(redact_passwords) do
    raise ex, "[%s] %s" % [tag, ex.message], ex.backtrace
  end
rescue => e
  handler[e]
end

#redact_passwordsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sq/dbsync/error_handler.rb', line 30

def redact_passwords
  lambda do |message|
    (
      config[:sources].values + [config[:target]]
    ).compact.inject(message) do |m, options|
      if options[:password]
        m.gsub(options[:password], 'REDACTED')
      else
        m
      end
    end
  end
end

#with_massaged_exception(*massagers) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/sq/dbsync/error_handler.rb', line 44

def with_massaged_exception(*massagers)
  yield
rescue => ex
  message = massagers.inject(ex.message) do |a, v|
    v.call(a)
  end

  raise ex, message, ex.backtrace
end

#wrap(&block) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/sq/dbsync/error_handler.rb', line 12

def wrap(&block)
  begin
    with_massaged_exception(redact_passwords, &block)
  rescue => ex
    handler[ex]

    raise ex
  end
end