From a3fa8cbe4784ce2422908234ce7f03f3c7ee8e85 Mon Sep 17 00:00:00 2001 From: xmdhs Date: Tue, 12 Sep 2023 12:58:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E5=8F=96=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handle/yggdrasil/error.go | 6 ++++++ handle/yggdrasil/session.go | 6 ++---- handle/yggdrasil/texture.go | 6 ++---- handle/yggdrasil/user.go | 21 +++++++-------------- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/handle/yggdrasil/error.go b/handle/yggdrasil/error.go index db4571c..7c5c136 100644 --- a/handle/yggdrasil/error.go +++ b/handle/yggdrasil/error.go @@ -16,3 +16,9 @@ func handleYgError(ctx context.Context, w http.ResponseWriter, e yggdrasil.Error } w.Write(b) } + +func (y *Yggdrasil) handleYgError(ctx context.Context, w http.ResponseWriter, err error) { + y.logger.WarnContext(ctx, err.Error()) + handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500) + return +} diff --git a/handle/yggdrasil/session.go b/handle/yggdrasil/session.go index 6c476e3..85094b6 100644 --- a/handle/yggdrasil/session.go +++ b/handle/yggdrasil/session.go @@ -19,8 +19,7 @@ func (y *Yggdrasil) SessionJoin() httprouter.Handle { } ip, err := utils.GetIP(r, y.config.RaelIP) if err != nil { - y.logger.WarnContext(ctx, err.Error()) - handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500) + y.handleYgError(ctx, w, err) return } err = y.yggdrasilService.SessionJoin(ctx, a, ip) @@ -30,8 +29,7 @@ func (y *Yggdrasil) SessionJoin() httprouter.Handle { handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: "Invalid token.", Error: "ForbiddenOperationException"}, 403) return } - y.logger.WarnContext(ctx, err.Error()) - handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500) + y.handleYgError(ctx, w, err) return } w.WriteHeader(204) diff --git a/handle/yggdrasil/texture.go b/handle/yggdrasil/texture.go index c95f565..bffaad6 100644 --- a/handle/yggdrasil/texture.go +++ b/handle/yggdrasil/texture.go @@ -108,8 +108,7 @@ func (y *Yggdrasil) PutTexture() httprouter.Handle { return } - y.logger.WarnContext(ctx, err.Error()) - handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500) + y.handleYgError(ctx, w, err) return } w.WriteHeader(204) @@ -145,8 +144,7 @@ func (y *Yggdrasil) DelTexture() httprouter.Handle { w.WriteHeader(401) return } - y.logger.WarnContext(ctx, err.Error()) - handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500) + y.handleYgError(ctx, w, err) return } w.WriteHeader(204) diff --git a/handle/yggdrasil/user.go b/handle/yggdrasil/user.go index d025f3c..ad531de 100644 --- a/handle/yggdrasil/user.go +++ b/handle/yggdrasil/user.go @@ -26,8 +26,7 @@ func (y *Yggdrasil) Authenticate() httprouter.Handle { handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: "Invalid credentials. Invalid username or password.", Error: "ForbiddenOperationException"}, 403) return } - y.logger.WarnContext(cxt, err.Error()) - handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500) + y.handleYgError(cxt, w, err) return } b, _ := json.Marshal(t) @@ -49,8 +48,7 @@ func (y *Yggdrasil) Validate() httprouter.Handle { handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: "Invalid token.", Error: "ForbiddenOperationException"}, 403) return } - y.logger.WarnContext(cxt, err.Error()) - handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500) + y.handleYgError(cxt, w, err) return } w.WriteHeader(204) @@ -71,8 +69,7 @@ func (y *Yggdrasil) Signout() httprouter.Handle { handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: "Invalid credentials. Invalid username or password.", Error: "ForbiddenOperationException"}, 403) return } - y.logger.WarnContext(cxt, err.Error()) - handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500) + y.handleYgError(cxt, w, err) return } w.WriteHeader(204) @@ -112,8 +109,7 @@ func (y *Yggdrasil) Refresh() httprouter.Handle { handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: "Invalid token.", Error: "ForbiddenOperationException"}, 403) return } - y.logger.WarnContext(cxt, err.Error()) - handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500) + y.handleYgError(cxt, w, err) return } b, _ := json.Marshal(t) @@ -147,8 +143,7 @@ func (y *Yggdrasil) GetProfile() httprouter.Handle { w.WriteHeader(204) return } - y.logger.WarnContext(ctx, err.Error()) - handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500) + y.handleYgError(ctx, w, err) return } b, _ := json.Marshal(u) @@ -170,8 +165,7 @@ func (y *Yggdrasil) BatchProfile() httprouter.Handle { } ul, err := y.yggdrasilService.BatchProfile(ctx, a) if err != nil { - y.logger.WarnContext(ctx, err.Error()) - handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500) + y.handleYgError(ctx, w, err) return } w.Write(lo.Must1(json.Marshal(ul))) @@ -192,8 +186,7 @@ func (y *Yggdrasil) PlayerCertificates() httprouter.Handle { handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: "Invalid token.", Error: "ForbiddenOperationException"}, 403) return } - y.logger.WarnContext(ctx, err.Error()) - handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500) + y.handleYgError(ctx, w, err) return } w.Write(lo.Must(json.Marshal(c)))