mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-08 22:13:26 +09:00
refactor: share package registry error status classification (#39133)
Signed-off-by: silverwind <me@silverwind.io>
This commit is contained in:
@@ -178,11 +178,7 @@ func DownloadPackageFile(ctx *context.Context) {
|
|||||||
ctx.Req.Method,
|
ctx.Req.Method,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
|
apiError(ctx, helper.PackageErrorStatus(err), err)
|
||||||
apiError(ctx, http.StatusNotFound, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
apiError(ctx, http.StatusInternalServerError, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -170,11 +170,7 @@ func DownloadPackageFile(ctx *context.Context) {
|
|||||||
ctx.Req.Method,
|
ctx.Req.Method,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
|
apiError(ctx, helper.PackageErrorStatus(err), err)
|
||||||
apiError(ctx, http.StatusNotFound, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
apiError(ctx, http.StatusInternalServerError, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -498,11 +498,7 @@ func downloadFile(ctx *context.Context, fileFilter container.Set[string], fileKe
|
|||||||
ctx.Req.Method,
|
ctx.Req.Method,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
|
apiError(ctx, helper.PackageErrorStatus(err), err)
|
||||||
apiError(ctx, http.StatusNotFound, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
apiError(ctx, http.StatusInternalServerError, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -68,11 +68,7 @@ func GetRepositoryFile(ctx *context.Context) {
|
|||||||
ctx.Req.Method,
|
ctx.Req.Method,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
|
apiError(ctx, helper.PackageErrorStatus(err), err)
|
||||||
apiError(ctx, http.StatusNotFound, err)
|
|
||||||
} else {
|
|
||||||
apiError(ctx, http.StatusInternalServerError, err)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,11 +43,7 @@ func DownloadPackageFile(ctx *context.Context) {
|
|||||||
ctx.Req.Method,
|
ctx.Req.Method,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
|
apiError(ctx, helper.PackageErrorStatus(err), err)
|
||||||
apiError(ctx, http.StatusNotFound, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
apiError(ctx, http.StatusInternalServerError, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,11 +176,7 @@ func DeletePackageFile(ctx *context.Context) {
|
|||||||
return pv, pf, nil
|
return pv, pf, nil
|
||||||
}()
|
}()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
|
apiError(ctx, helper.PackageErrorStatus(err), err)
|
||||||
apiError(ctx, http.StatusNotFound, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
apiError(ctx, http.StatusInternalServerError, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
package helper
|
package helper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -12,9 +13,18 @@ import (
|
|||||||
packages_model "gitea.dev/models/packages"
|
packages_model "gitea.dev/models/packages"
|
||||||
"gitea.dev/modules/log"
|
"gitea.dev/modules/log"
|
||||||
"gitea.dev/modules/setting"
|
"gitea.dev/modules/setting"
|
||||||
|
"gitea.dev/modules/util"
|
||||||
"gitea.dev/services/context"
|
"gitea.dev/services/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// PackageErrorStatus returns the status to report for a package lookup error
|
||||||
|
func PackageErrorStatus(err error) int {
|
||||||
|
if errors.Is(err, util.ErrNotExist) {
|
||||||
|
return http.StatusNotFound
|
||||||
|
}
|
||||||
|
return http.StatusInternalServerError
|
||||||
|
}
|
||||||
|
|
||||||
// ProcessErrorForUser logs the error and returns a user-error message for the end user.
|
// 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.
|
// 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 {
|
func ProcessErrorForUser(ctx *context.Context, status int, errObj any) string {
|
||||||
|
|||||||
@@ -99,11 +99,7 @@ func DownloadPackageFile(ctx *context.Context) {
|
|||||||
ctx.Req.Method,
|
ctx.Req.Method,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
|
apiError(ctx, helper.PackageErrorStatus(err), err)
|
||||||
apiError(ctx, http.StatusNotFound, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
apiError(ctx, http.StatusInternalServerError, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -414,11 +414,7 @@ func DownloadPackageFile(ctx *context.Context) {
|
|||||||
ctx.Req.Method,
|
ctx.Req.Method,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
|
apiError(ctx, helper.PackageErrorStatus(err), err)
|
||||||
apiError(ctx, http.StatusNotFound, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
apiError(ctx, http.StatusInternalServerError, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -667,11 +663,7 @@ func DownloadSymbolFile(ctx *context.Context) {
|
|||||||
|
|
||||||
s, u, pf, err := packages_service.OpenFileForDownload(ctx, pfs[0], ctx.Req.Method)
|
s, u, pf, err := packages_service.OpenFileForDownload(ctx, pfs[0], ctx.Req.Method)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
|
apiError(ctx, helper.PackageErrorStatus(err), err)
|
||||||
apiError(ctx, http.StatusNotFound, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
apiError(ctx, http.StatusInternalServerError, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ package pypi
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -95,11 +94,7 @@ func DownloadPackageFile(ctx *context.Context) {
|
|||||||
ctx.Req.Method,
|
ctx.Req.Method,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
|
apiError(ctx, helper.PackageErrorStatus(err), err)
|
||||||
apiError(ctx, http.StatusNotFound, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
apiError(ctx, http.StatusInternalServerError, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,11 +70,7 @@ func streamState(ctx *context.Context, name, serial string) {
|
|||||||
ctx.Req.Method,
|
ctx.Req.Method,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
|
apiError(ctx, helper.PackageErrorStatus(err), err)
|
||||||
apiError(ctx, http.StatusNotFound, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
apiError(ctx, http.StatusInternalServerError, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
package vagrant
|
package vagrant
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -231,11 +230,7 @@ func DownloadPackageFile(ctx *context.Context) {
|
|||||||
ctx.Req.Method,
|
ctx.Req.Method,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, packages_model.ErrPackageNotExist) || errors.Is(err, packages_model.ErrPackageFileNotExist) {
|
apiError(ctx, helper.PackageErrorStatus(err), err)
|
||||||
apiError(ctx, http.StatusNotFound, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
apiError(ctx, http.StatusInternalServerError, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user