完善错误处理逻辑,统一使用 BizErr 包装业务错误,提供打印源码跳转并返回合适的 http 状态码

This commit is contained in:
2025-05-24 12:37:16 +08:00
parent 928d78d41b
commit 1e7b5777a2
11 changed files with 203 additions and 87 deletions

View File

@@ -16,21 +16,19 @@ func Init() {
switch env.RunMode {
case "debug":
handler = tint.NewHandler(writer, &tint.Options{
AddSource: true,
Level: env.LogLevel,
TimeFormat: timeFormat,
ReplaceAttr: func(_ []string, attr slog.Attr) slog.Attr {
err, ok := attr.Value.Any().(error)
if ok {
return tint.Err(err)
switch v := attr.Value.Any().(type) {
case error:
return tint.Err(v)
}
return attr
},
})
case "production":
handler = slog.NewJSONHandler(writer, &slog.HandlerOptions{
AddSource: false,
Level: env.LogLevel,
Level: env.LogLevel,
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
if a.Key == "time" {
return slog.String("time", a.Value.Time().Format(timeFormat))