Skip to content

Commit

Permalink
json格式错误检查输出详细信息
Browse files Browse the repository at this point in the history
  • Loading branch information
fruitbars committed Aug 13, 2024
1 parent 5689f6d commit 2ebd49a
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,17 @@ func InitConfig(configName string) error {
}

} else if ftype == "json" {

err = json.Unmarshal(data, &conf)
if err != nil {
log.Println(err)

if syntaxErr, ok := err.(*json.SyntaxError); ok {
line, character := FindLineAndCharacter(data, int(syntaxErr.Offset))
log.Printf("JSON 语法错误在第 %d 行,第 %d 个字符附近: %v\n", line, character, err)
log.Printf("上下文: %s\n", GetErrorContext(data, int(syntaxErr.Offset)))
} else {
log.Printf("JSON 解析错误: %v\n", err)
}
}
} else {
log.Println("unsupport config type:", ftype)
Expand Down

0 comments on commit 2ebd49a

Please sign in to comment.