1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-25 19:58:36 +00:00

Improve package API log handling (#35100)

Simplify code and fix log processing logic
This commit is contained in:
wxiaoguang
2025-07-17 00:25:49 +08:00
committed by GitHub
parent 37958e486a
commit f0da1de7e3
23 changed files with 101 additions and 132 deletions

View File

@@ -15,31 +15,29 @@ import (
"code.gitea.io/gitea/services/context"
)
// LogAndProcessError logs an error and calls a custom callback with the processed error message.
// If the error is an InternalServerError the message is stripped if the user is not an admin.
func LogAndProcessError(ctx *context.Context, status int, obj any, cb func(string)) {
// ProcessErrorForUser logs the error and returns a user-error message for the end user.
// If the status is http.StatusInternalServerError, the message is stripped for non-admin users in production.
func ProcessErrorForUser(ctx *context.Context, status int, errObj any) string {
var message string
if err, ok := obj.(error); ok {
if err, ok := errObj.(error); ok {
message = err.Error()
} else if obj != nil {
message = fmt.Sprintf("%s", obj)
} else if errObj != nil {
message = fmt.Sprint(errObj)
}
if status == http.StatusInternalServerError {
log.ErrorWithSkip(1, message)
log.Log(2, log.ERROR, "Package registry API internal error: %d %s", status, message)
if setting.IsProd && (ctx.Doer == nil || !ctx.Doer.IsAdmin) {
message = ""
message = "internal server error"
}
} else {
log.Debug(message)
return message
}
if cb != nil {
cb(message)
}
log.Log(2, log.DEBUG, "Package registry API user error: %d %s", status, message)
return message
}
// Serves the content of the package file
// ServePackageFile the content of the package file
// If the url is set it will redirect the request, otherwise the content is copied to the response.
func ServePackageFile(ctx *context.Context, s io.ReadSeekCloser, u *url.URL, pf *packages_model.PackageFile, forceOpts ...*context.ServeHeaderOptions) {
if u != nil {