Do not panic when a regex doesn't compile

Show proper error message instead
fix #58
This commit is contained in:
ppom 2024-01-04 12:00:00 +01:00
parent f662a485b9
commit 9b766aa2b8
2 changed files with 8 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package app
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"os" "os"
"regexp" "regexp"
"strings" "strings"
@ -26,7 +27,7 @@ func (c *Conf) setup() {
compiled, err := regexp.Compile(fmt.Sprintf("^%v$", pattern.Regex)) compiled, err := regexp.Compile(fmt.Sprintf("^%v$", pattern.Regex))
if err != nil { 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) c.Patterns[patternName].Regex = fmt.Sprintf("(?P<%s>%s)", patternName, pattern.Regex)
for _, ignore := range pattern.Ignore { for _, ignore := range pattern.Ignore {
@ -97,8 +98,11 @@ func (c *Conf) setup() {
regex = strings.Replace(regex, pattern.nameWithBraces, pattern.Regex, 1) regex = strings.Replace(regex, pattern.nameWithBraces, pattern.Regex, 1)
} }
} }
// TODO regexp.Compile and show proper message if it doesn't instead of panicing compiledRegex, err := regexp.Compile(regex)
filter.compiledRegex = append(filter.compiledRegex, *regexp.MustCompile(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 { if len(filter.Actions) == 0 {

View File

@ -76,6 +76,5 @@ func Fatalln(args ...any) {
} }
func Fatalf(format string, args ...any) { func Fatalf(format string, args ...any) {
level := FATAL log.Fatalf(FATAL.String()+format, args)
log.Fatalf(level.String()+format, args)
} }