diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 7a626a70659b8..bc21de3b83da7 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -622,26 +622,47 @@ func SearchRepo(ctx *context.Context) { } } - var err error + getErrorStr := func(ctx *context.Context, err error, fallbackStr string) string { + if ctx.Doer == nil || !ctx.Doer.IsAdmin { + return fallbackStr + } + return fmt.Sprintf("%s: %v", fallbackStr, err) + } + + // To improve performance when only the count is requested + if ctx.FormBool("count_only") { + count, err := repo_model.CountRepository(ctx, opts) + if err != nil { + log.Error("CountRepository: %v", err) + ctx.JSON(http.StatusInternalServerError, api.SearchError{ + OK: false, + Error: getErrorStr(ctx, err, "CountRepository internal error"), + }) + return + } + ctx.SetTotalCountHeader(count) + return + } + repos, count, err := repo_model.SearchRepository(ctx, opts) if err != nil { + log.Error("SearchRepository: %v", err) ctx.JSON(http.StatusInternalServerError, api.SearchError{ OK: false, - Error: err.Error(), + Error: getErrorStr(ctx, err, "SearchRepository internal error"), }) return } ctx.SetTotalCountHeader(count) - // To improve performance when only the count is requested - if ctx.FormBool("count_only") { - return - } - latestCommitStatuses, err := commitstatus_service.FindReposLastestCommitStatuses(ctx, repos) if err != nil { log.Error("FindReposLastestCommitStatuses: %v", err) + ctx.JSON(http.StatusInternalServerError, api.SearchError{ + OK: false, + Error: getErrorStr(ctx, err, "FindReposLastestCommitStatuses internal error"), + }) return }