mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-03 20:36:07 +01:00 
			
		
		
		
	Refactor error system (#33610)
This commit is contained in:
		@@ -14,15 +14,15 @@ import (
 | 
			
		||||
// UserAssignmentWeb returns a middleware to handle context-user assignment for web routes
 | 
			
		||||
func UserAssignmentWeb() func(ctx *Context) {
 | 
			
		||||
	return func(ctx *Context) {
 | 
			
		||||
		errorFn := func(status int, title string, obj any) {
 | 
			
		||||
		errorFn := func(status int, obj any) {
 | 
			
		||||
			err, ok := obj.(error)
 | 
			
		||||
			if !ok {
 | 
			
		||||
				err = fmt.Errorf("%s", obj)
 | 
			
		||||
			}
 | 
			
		||||
			if status == http.StatusNotFound {
 | 
			
		||||
				ctx.NotFound(title, err)
 | 
			
		||||
				ctx.NotFound(err)
 | 
			
		||||
			} else {
 | 
			
		||||
				ctx.ServerError(title, err)
 | 
			
		||||
				ctx.ServerError("UserAssignmentWeb", err)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		ctx.ContextUser = userAssignment(ctx.Base, ctx.Doer, errorFn)
 | 
			
		||||
@@ -42,9 +42,9 @@ func UserIDAssignmentAPI() func(ctx *APIContext) {
 | 
			
		||||
			ctx.ContextUser, err = user_model.GetUserByID(ctx, userID)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				if user_model.IsErrUserNotExist(err) {
 | 
			
		||||
					ctx.Error(http.StatusNotFound, "GetUserByID", err)
 | 
			
		||||
					ctx.APIError(http.StatusNotFound, err)
 | 
			
		||||
				} else {
 | 
			
		||||
					ctx.Error(http.StatusInternalServerError, "GetUserByID", err)
 | 
			
		||||
					ctx.APIError(http.StatusInternalServerError, err)
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
@@ -54,11 +54,11 @@ func UserIDAssignmentAPI() func(ctx *APIContext) {
 | 
			
		||||
// UserAssignmentAPI returns a middleware to handle context-user assignment for api routes
 | 
			
		||||
func UserAssignmentAPI() func(ctx *APIContext) {
 | 
			
		||||
	return func(ctx *APIContext) {
 | 
			
		||||
		ctx.ContextUser = userAssignment(ctx.Base, ctx.Doer, ctx.Error)
 | 
			
		||||
		ctx.ContextUser = userAssignment(ctx.Base, ctx.Doer, ctx.APIError)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func userAssignment(ctx *Base, doer *user_model.User, errCb func(int, string, any)) (contextUser *user_model.User) {
 | 
			
		||||
func userAssignment(ctx *Base, doer *user_model.User, errCb func(int, any)) (contextUser *user_model.User) {
 | 
			
		||||
	username := ctx.PathParam("username")
 | 
			
		||||
 | 
			
		||||
	if doer != nil && doer.LowerName == strings.ToLower(username) {
 | 
			
		||||
@@ -71,12 +71,12 @@ func userAssignment(ctx *Base, doer *user_model.User, errCb func(int, string, an
 | 
			
		||||
				if redirectUserID, err := user_model.LookupUserRedirect(ctx, username); err == nil {
 | 
			
		||||
					RedirectToUser(ctx, username, redirectUserID)
 | 
			
		||||
				} else if user_model.IsErrUserRedirectNotExist(err) {
 | 
			
		||||
					errCb(http.StatusNotFound, "GetUserByName", err)
 | 
			
		||||
					errCb(http.StatusNotFound, err)
 | 
			
		||||
				} else {
 | 
			
		||||
					errCb(http.StatusInternalServerError, "LookupUserRedirect", err)
 | 
			
		||||
					errCb(http.StatusInternalServerError, fmt.Errorf("LookupUserRedirect: %w", err))
 | 
			
		||||
				}
 | 
			
		||||
			} else {
 | 
			
		||||
				errCb(http.StatusInternalServerError, "GetUserByName", err)
 | 
			
		||||
				errCb(http.StatusInternalServerError, fmt.Errorf("GetUserByName: %w", err))
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user