Add error text to exception when an error was logged

This commit is contained in:
Lars Kanis 2022-12-02 13:18:28 +01:00
parent bd4310a080
commit ee81807f49
3 changed files with 13 additions and 8 deletions

View File

@ -9,7 +9,8 @@ module PgLdapSync
class ApplicationExit < RuntimeError class ApplicationExit < RuntimeError
attr_reader :exitcode attr_reader :exitcode
def initialize(exitcode) def initialize(exitcode, error=nil)
super(error)
@exitcode = exitcode @exitcode = exitcode
end end
end end

View File

@ -384,14 +384,14 @@ class Application
# Determine exitcode # Determine exitcode
if log.had_errors? if log.had_errors?
raise ErrorExit, 1 raise ErrorExit.new(1, log.first_error)
end end
end end
def self.run(argv) def self.run(argv)
s = self.new s = self.new
s.config_fname = '/etc/pg_ldap_sync.yaml' s.config_fname = '/etc/pg_ldap_sync.yaml'
s.log = Logger.new($stdout, @error_counters) s.log = Logger.new($stdout)
s.log.level = Logger::ERROR s.log.level = Logger::ERROR
OptionParser.new do |opts| OptionParser.new do |opts|

View File

@ -2,23 +2,27 @@ require 'logger'
module PgLdapSync module PgLdapSync
class Logger < ::Logger class Logger < ::Logger
def initialize(io, counters) def initialize(io)
super(io) super(io)
@counters = {} @counters = {}
end end
def add(severity, *args) def add(severity, *args, &block)
@counters[severity] ||= 0 return unless [Logger::FATAL, Logger::ERROR].include?(severity)
@counters[severity] += 1 @counters[severity] ||= block ? block.call : args.first
super super
end end
def had_logged?(severity) def had_logged?(severity)
@counters[severity] && @counters[severity] > 0 !!@counters[severity]
end end
def had_errors? def had_errors?
had_logged?(Logger::FATAL) || had_logged?(Logger::ERROR) had_logged?(Logger::FATAL) || had_logged?(Logger::ERROR)
end end
def first_error
@counters[Logger::FATAL] || @counters[Logger::ERROR]
end
end end
end end