Skip to content

Refactoring packages error processing #27979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
6 changes: 3 additions & 3 deletions routers/api/packages/alpine/alpine.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ func UploadPackageFile(ctx *context.Context) {
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageVersion, packages_model.ErrDuplicatePackageFile:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
6 changes: 3 additions & 3 deletions routers/api/packages/cargo/cargo.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,10 @@ func UploadPackage(ctx *context.Context) {
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageVersion:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
6 changes: 3 additions & 3 deletions routers/api/packages/chef/chef.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,10 @@ func UploadPackage(ctx *context.Context) {
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageVersion:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
6 changes: 3 additions & 3 deletions routers/api/packages/composer/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ func UploadPackage(ctx *context.Context) {
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageVersion:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
8 changes: 5 additions & 3 deletions routers/api/packages/conan/conan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package conan

import (
std_ctx "context"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -21,6 +22,7 @@ import (
packages_module "code.gitea.io/gitea/modules/packages"
conan_module "code.gitea.io/gitea/modules/packages/conan"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/api/packages/helper"
notify_service "code.gitea.io/gitea/services/notify"
packages_service "code.gitea.io/gitea/services/packages"
Expand Down Expand Up @@ -413,10 +415,10 @@ func uploadFile(ctx *context.Context, fileFilter container.Set[string], fileKey
pfci,
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageFile:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
6 changes: 3 additions & 3 deletions routers/api/packages/conda/conda.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ func UploadPackageFile(ctx *context.Context) {
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageFile:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
16 changes: 7 additions & 9 deletions routers/api/packages/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,17 +546,15 @@ func UploadManifest(ctx *context.Context) {
digest, err := processManifest(ctx, mci, buf)
if err != nil {
var namedError *namedError
if errors.As(err, &namedError) {
switch {
case errors.As(err, &namedError):
apiErrorDefined(ctx, namedError)
} else if errors.Is(err, container_model.ErrContainerBlobNotExist) {
case errors.Is(err, container_model.ErrContainerBlobNotExist):
apiErrorDefined(ctx, errBlobUnknown)
} else {
switch err {
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
}
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
}
return
}
Expand Down
6 changes: 3 additions & 3 deletions routers/api/packages/cran/cran.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ func uploadPackageFile(ctx *context.Context, compositeKey string, properties map
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageFile:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
6 changes: 3 additions & 3 deletions routers/api/packages/debian/debian.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ func UploadPackageFile(ctx *context.Context) {
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageVersion, packages_model.ErrDuplicatePackageFile:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
7 changes: 4 additions & 3 deletions routers/api/packages/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
packages_module "code.gitea.io/gitea/modules/packages"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/api/packages/helper"
packages_service "code.gitea.io/gitea/services/packages"
)
Expand Down Expand Up @@ -109,10 +110,10 @@ func UploadPackage(ctx *context.Context) {
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageFile:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
6 changes: 3 additions & 3 deletions routers/api/packages/goproxy/goproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ func UploadPackage(ctx *context.Context) {
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageVersion:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
6 changes: 3 additions & 3 deletions routers/api/packages/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ func UploadPackage(ctx *context.Context) {
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageVersion:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
7 changes: 4 additions & 3 deletions routers/api/packages/maven/maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"code.gitea.io/gitea/modules/log"
packages_module "code.gitea.io/gitea/modules/packages"
maven_module "code.gitea.io/gitea/modules/packages/maven"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/api/packages/helper"
packages_service "code.gitea.io/gitea/services/packages"

Expand Down Expand Up @@ -361,10 +362,10 @@ func UploadPackageFile(ctx *context.Context) {
pfci,
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageFile:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
6 changes: 3 additions & 3 deletions routers/api/packages/npm/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ func UploadPackage(ctx *context.Context) {
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageVersion:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
6 changes: 3 additions & 3 deletions routers/api/packages/nuget/nuget.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,10 @@ func UploadPackage(ctx *context.Context) {
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageVersion:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
6 changes: 3 additions & 3 deletions routers/api/packages/pub/pub.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ func UploadPackageFile(ctx *context.Context) {
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageVersion:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
8 changes: 5 additions & 3 deletions routers/api/packages/pypi/pypi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package pypi

import (
"encoding/hex"
"errors"
"io"
"net/http"
"regexp"
Expand All @@ -16,6 +17,7 @@ import (
packages_module "code.gitea.io/gitea/modules/packages"
pypi_module "code.gitea.io/gitea/modules/packages/pypi"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/validation"
"code.gitea.io/gitea/routers/api/packages/helper"
packages_service "code.gitea.io/gitea/services/packages"
Expand Down Expand Up @@ -175,10 +177,10 @@ func UploadPackageFile(ctx *context.Context) {
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageFile:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
6 changes: 3 additions & 3 deletions routers/api/packages/rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ func UploadPackageFile(ctx *context.Context) {
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageVersion, packages_model.ErrDuplicatePackageFile:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
6 changes: 3 additions & 3 deletions routers/api/packages/rubygems/rubygems.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ func UploadPackageFile(ctx *context.Context) {
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageVersion:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
6 changes: 3 additions & 3 deletions routers/api/packages/swift/swift.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,10 @@ func UploadPackageFile(ctx *context.Context) {
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageVersion:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
8 changes: 5 additions & 3 deletions routers/api/packages/vagrant/vagrant.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package vagrant

import (
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -16,6 +17,7 @@ import (
packages_module "code.gitea.io/gitea/modules/packages"
vagrant_module "code.gitea.io/gitea/modules/packages/vagrant"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers/api/packages/helper"
packages_service "code.gitea.io/gitea/services/packages"

Expand Down Expand Up @@ -202,10 +204,10 @@ func UploadPackageFile(ctx *context.Context) {
},
)
if err != nil {
switch err {
case packages_model.ErrDuplicatePackageFile:
switch {
case errors.Is(err, util.ErrAlreadyExist):
apiError(ctx, http.StatusConflict, err)
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize:
case errors.Is(err, util.ErrInvalidArgument):
apiError(ctx, http.StatusForbidden, err)
default:
apiError(ctx, http.StatusInternalServerError, err)
Expand Down
6 changes: 3 additions & 3 deletions services/packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
)

var (
ErrQuotaTypeSize = errors.New("maximum allowed package type size exceeded")
ErrQuotaTotalSize = errors.New("maximum allowed package storage quota exceeded")
ErrQuotaTotalCount = errors.New("maximum allowed package count exceeded")
ErrQuotaTypeSize = util.NewInvalidArgumentErrorf("maximum allowed package type size exceeded")
ErrQuotaTotalSize = util.NewInvalidArgumentErrorf("maximum allowed package storage quota exceeded")
ErrQuotaTotalCount = util.NewInvalidArgumentErrorf("maximum allowed package count exceeded")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "you have reached a limit" an "argument error"?

Copy link
Author

@ghost ghost Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KN4CK3R

No. Added draft with FailedPrecondition (similar with gRPC spec) error in this branch. Would it be valid for current scenario?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a mistake that only ErrQuotaTotalCount is a util.NewFailedPreconditionErrorf?

)

// PackageInfo describes a package
Expand Down