From ee81807f494922e921e5217d78e4ca8e703f06c7 Mon Sep 17 00:00:00 2001 From: Lars Kanis Date: Fri, 2 Dec 2022 13:18:28 +0100 Subject: [PATCH] Add error text to exception when an error was logged --- lib/pg_ldap_sync.rb | 3 ++- lib/pg_ldap_sync/application.rb | 4 ++-- lib/pg_ldap_sync/logger.rb | 14 +++++++++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/pg_ldap_sync.rb b/lib/pg_ldap_sync.rb index 8d0a5e0..aa0cc86 100644 --- a/lib/pg_ldap_sync.rb +++ b/lib/pg_ldap_sync.rb @@ -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 diff --git a/lib/pg_ldap_sync/application.rb b/lib/pg_ldap_sync/application.rb index 252140b..25a3e8f 100644 --- a/lib/pg_ldap_sync/application.rb +++ b/lib/pg_ldap_sync/application.rb @@ -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| diff --git a/lib/pg_ldap_sync/logger.rb b/lib/pg_ldap_sync/logger.rb index 5fa43cd..8219cb8 100644 --- a/lib/pg_ldap_sync/logger.rb +++ b/lib/pg_ldap_sync/logger.rb @@ -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