提取错误处理

This commit is contained in:
xmdhs 2023-09-12 12:58:09 +08:00
parent c50289ce64
commit a3fa8cbe47
No known key found for this signature in database
GPG Key ID: E809D6D43DEFCC95
4 changed files with 17 additions and 22 deletions

View File

@ -16,3 +16,9 @@ func handleYgError(ctx context.Context, w http.ResponseWriter, e yggdrasil.Error
} }
w.Write(b) 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
}

View File

@ -19,8 +19,7 @@ func (y *Yggdrasil) SessionJoin() httprouter.Handle {
} }
ip, err := utils.GetIP(r, y.config.RaelIP) ip, err := utils.GetIP(r, y.config.RaelIP)
if err != nil { if err != nil {
y.logger.WarnContext(ctx, err.Error()) y.handleYgError(ctx, w, err)
handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500)
return return
} }
err = y.yggdrasilService.SessionJoin(ctx, a, ip) 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) handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: "Invalid token.", Error: "ForbiddenOperationException"}, 403)
return return
} }
y.logger.WarnContext(ctx, err.Error()) y.handleYgError(ctx, w, err)
handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500)
return return
} }
w.WriteHeader(204) w.WriteHeader(204)

View File

@ -108,8 +108,7 @@ func (y *Yggdrasil) PutTexture() httprouter.Handle {
return return
} }
y.logger.WarnContext(ctx, err.Error()) y.handleYgError(ctx, w, err)
handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500)
return return
} }
w.WriteHeader(204) w.WriteHeader(204)
@ -145,8 +144,7 @@ func (y *Yggdrasil) DelTexture() httprouter.Handle {
w.WriteHeader(401) w.WriteHeader(401)
return return
} }
y.logger.WarnContext(ctx, err.Error()) y.handleYgError(ctx, w, err)
handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500)
return return
} }
w.WriteHeader(204) w.WriteHeader(204)

View File

@ -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) handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: "Invalid credentials. Invalid username or password.", Error: "ForbiddenOperationException"}, 403)
return return
} }
y.logger.WarnContext(cxt, err.Error()) y.handleYgError(cxt, w, err)
handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500)
return return
} }
b, _ := json.Marshal(t) 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) handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: "Invalid token.", Error: "ForbiddenOperationException"}, 403)
return return
} }
y.logger.WarnContext(cxt, err.Error()) y.handleYgError(cxt, w, err)
handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500)
return return
} }
w.WriteHeader(204) 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) handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: "Invalid credentials. Invalid username or password.", Error: "ForbiddenOperationException"}, 403)
return return
} }
y.logger.WarnContext(cxt, err.Error()) y.handleYgError(cxt, w, err)
handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500)
return return
} }
w.WriteHeader(204) w.WriteHeader(204)
@ -112,8 +109,7 @@ func (y *Yggdrasil) Refresh() httprouter.Handle {
handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: "Invalid token.", Error: "ForbiddenOperationException"}, 403) handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: "Invalid token.", Error: "ForbiddenOperationException"}, 403)
return return
} }
y.logger.WarnContext(cxt, err.Error()) y.handleYgError(cxt, w, err)
handleYgError(cxt, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500)
return return
} }
b, _ := json.Marshal(t) b, _ := json.Marshal(t)
@ -147,8 +143,7 @@ func (y *Yggdrasil) GetProfile() httprouter.Handle {
w.WriteHeader(204) w.WriteHeader(204)
return return
} }
y.logger.WarnContext(ctx, err.Error()) y.handleYgError(ctx, w, err)
handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500)
return return
} }
b, _ := json.Marshal(u) b, _ := json.Marshal(u)
@ -170,8 +165,7 @@ func (y *Yggdrasil) BatchProfile() httprouter.Handle {
} }
ul, err := y.yggdrasilService.BatchProfile(ctx, a) ul, err := y.yggdrasilService.BatchProfile(ctx, a)
if err != nil { if err != nil {
y.logger.WarnContext(ctx, err.Error()) y.handleYgError(ctx, w, err)
handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500)
return return
} }
w.Write(lo.Must1(json.Marshal(ul))) 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) handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: "Invalid token.", Error: "ForbiddenOperationException"}, 403)
return return
} }
y.logger.WarnContext(ctx, err.Error()) y.handleYgError(ctx, w, err)
handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500)
return return
} }
w.Write(lo.Must(json.Marshal(c))) w.Write(lo.Must(json.Marshal(c)))