Add context for all exceptions

Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
2026-01-21 19:37:20 +08:00
parent 154c929859
commit 986f63c0af
16 changed files with 91 additions and 87 deletions

View File

@@ -1,6 +1,7 @@
package exception
import (
"context"
"fmt"
)
@@ -50,7 +51,7 @@ func (self *Builder) SetError(e error) *Builder {
return self
}
func (self *Builder) Build() string {
func (self *Builder) Build(ctx context.Context) string {
errorCode := fmt.Sprintf("%s%s%s%s%s",
self.Status,
self.Service,
@@ -59,7 +60,7 @@ func (self *Builder) Build() string {
self.Original,
)
if self.Error != nil {
ErrorHandler(self.Status, errorCode, self.Error)
ErrorHandler(ctx, self.Status, errorCode, self.Error)
}
return errorCode
}

View File

@@ -1,16 +1,19 @@
package exception
import "log/slog"
import (
"context"
"log/slog"
)
func ErrorHandler(status string, errorCode string, err error) {
func ErrorHandler(ctx context.Context, status string, errorCode string, err error) {
switch status {
case StatusSuccess:
slog.Info("Service exception", "id", errorCode, "err", err)
slog.InfoContext(ctx, "Service exception! ErrId: "+errorCode, "id", errorCode, "err", err)
case StatusUser:
slog.Warn("Service exception", "id", errorCode, "err", err)
slog.WarnContext(ctx, "Service exception! ErrId: "+errorCode, "id", errorCode, "err", err)
case StatusServer:
slog.Error("Service exception", "id", errorCode, "err", err)
slog.ErrorContext(ctx, "Service exception! ErrId: "+errorCode, "id", errorCode, "err", err)
case StatusClient:
slog.Error("Service exception", "id", errorCode, "err", err)
slog.ErrorContext(ctx, "Service exception! ErrId: "+errorCode, "id", errorCode, "err", err)
}
}