fix(parser/log): handle non json input
All checks were successful
submodules sync / sync (push) Successful in 50s
build / build (push) Successful in 2m54s
build / trigger-build-image (push) Successful in 18s

This commit is contained in:
张泊明518370910136 2025-11-27 22:09:49 -08:00
parent 8155de0c92
commit e800bc5c2a
GPG Key ID: D47306D7062CDA9D

View File

@ -12,24 +12,35 @@ import (
func (*Log) parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult { func (*Log) parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
content := executorResult.Files[conf.Filename] content := executorResult.Files[conf.Filename]
var data map[string]any var data map[string]any
err := json.Unmarshal([]byte(content), &data) contentBytes := []byte(content)
if err != nil { if json.Valid(contentBytes) {
slog.Error(conf.Msg, "error", err) err := json.Unmarshal(contentBytes, &data)
return stage.ParserResult{ if err != nil {
Score: 0, slog.Error(conf.Msg, "error", err)
Comment: fmt.Sprintf("Failed to parse content: %s", err), return stage.ParserResult{
Score: 0,
Comment: fmt.Sprintf("Failed to parse content: %s", err),
}
} }
args := make([]any, 0, len(data)*2)
for key, value := range data {
args = append(args, key, value)
}
slog.Default().Log(
context.Background(),
slog.Level(conf.Level),
conf.Msg,
args...,
)
} else {
slog.Default().Log(
context.Background(),
slog.Level(conf.Level),
conf.Msg,
"content",
content,
)
} }
args := make([]any, 0, len(data)*2)
for key, value := range data {
args = append(args, key, value)
}
slog.Default().Log(
context.Background(),
slog.Level(conf.Level),
conf.Msg,
args...,
)
return stage.ParserResult{ return stage.ParserResult{
Score: 0, Score: 0,
Comment: "", Comment: "",