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
attr_reader :exitcode
def initialize(exitcode)
def initialize(exitcode, error=nil)
super(error)
@exitcode = exitcode
end
end

View File

@ -384,14 +384,14 @@ class Application
# Determine exitcode
if log.had_errors?
raise ErrorExit, 1
raise ErrorExit.new(1, log.first_error)
end
end
def self.run(argv)
s = self.new
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
OptionParser.new do |opts|

View File

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