From 9b766aa2b862f5a57b4c15147c92b4c999a310f0 Mon Sep 17 00:00:00 2001 From: ppom <> Date: Thu, 4 Jan 2024 12:00:00 +0100 Subject: [PATCH] Do not panic when a regex doesn't compile Show proper error message instead fix #58 --- app/startup.go | 10 +++++++--- logger/log.go | 3 +-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/startup.go b/app/startup.go index 3a3436f..54d9097 100644 --- a/app/startup.go +++ b/app/startup.go @@ -3,6 +3,7 @@ package app import ( "encoding/json" "fmt" + "log" "os" "regexp" "strings" @@ -26,7 +27,7 @@ func (c *Conf) setup() { compiled, err := regexp.Compile(fmt.Sprintf("^%v$", pattern.Regex)) if err != nil { - logger.Fatalf("Bad configuration: pattern %v doesn't compile!", patternName) + logger.Fatalf("Bad configuration: pattern %v: %v", patternName, err) } c.Patterns[patternName].Regex = fmt.Sprintf("(?P<%s>%s)", patternName, pattern.Regex) for _, ignore := range pattern.Ignore { @@ -97,8 +98,11 @@ func (c *Conf) setup() { regex = strings.Replace(regex, pattern.nameWithBraces, pattern.Regex, 1) } } - // TODO regexp.Compile and show proper message if it doesn't instead of panicing - filter.compiledRegex = append(filter.compiledRegex, *regexp.MustCompile(regex)) + compiledRegex, err := regexp.Compile(regex) + if err != nil { + log.Fatalf("%vBad configuration: regex of filter %s.%s: %v", logger.FATAL, stream.name, filter.name, err) + } + filter.compiledRegex = append(filter.compiledRegex, *compiledRegex) } if len(filter.Actions) == 0 { diff --git a/logger/log.go b/logger/log.go index 221d45b..fdc6d8e 100644 --- a/logger/log.go +++ b/logger/log.go @@ -76,6 +76,5 @@ func Fatalln(args ...any) { } func Fatalf(format string, args ...any) { - level := FATAL - log.Fatalf(level.String()+format, args) + log.Fatalf(FATAL.String()+format, args) }