Skip to content

Commit

Permalink
Use ErrInvalidArgument in packages (#22268)
Browse files Browse the repository at this point in the history
Related to
#22262 (comment)

Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: Lauris BH <lauris@nix.lv>
  • Loading branch information
4 people authored Dec 31, 2022
1 parent dce8887 commit 3fef47b
Show file tree
Hide file tree
Showing 37 changed files with 144 additions and 94 deletions.
5 changes: 2 additions & 3 deletions models/asymkey/ssh_key_fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package asymkey

import (
"context"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -59,9 +58,9 @@ func calcFingerprintSSHKeygen(publicKeyContent string) (string, error) {
if strings.Contains(stderr, "is not a public key file") {
return "", ErrKeyUnableVerify{stderr}
}
return "", fmt.Errorf("'ssh-keygen -lf %s' failed with error '%s': %s", tmpPath, err, stderr)
return "", util.NewInvalidArgumentErrorf("'ssh-keygen -lf %s' failed with error '%s': %s", tmpPath, err, stderr)
} else if len(stdout) < 2 {
return "", errors.New("not enough output for calculating fingerprint: " + stdout)
return "", util.NewInvalidArgumentErrorf("not enough output for calculating fingerprint: %s", stdout)
}
return strings.Split(stdout, " ")[1], nil
}
Expand Down
5 changes: 2 additions & 3 deletions models/asymkey/ssh_key_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/base64"
"encoding/binary"
"encoding/pem"
"errors"
"fmt"
"math/big"
"os"
Expand Down Expand Up @@ -122,7 +121,7 @@ func parseKeyString(content string) (string, error) {
parts := strings.SplitN(content, " ", 3)
switch len(parts) {
case 0:
return "", errors.New("empty key")
return "", util.NewInvalidArgumentErrorf("empty key")
case 1:
keyContent = parts[0]
case 2:
Expand Down Expand Up @@ -167,7 +166,7 @@ func CheckPublicKeyString(content string) (_ string, err error) {

content = strings.TrimRight(content, "\n\r")
if strings.ContainsAny(content, "\n\r") {
return "", errors.New("only a single line with a single key please")
return "", util.NewInvalidArgumentErrorf("only a single line with a single key please")
}

// remove any unnecessary whitespace now
Expand Down
4 changes: 2 additions & 2 deletions models/asymkey/ssh_key_principals.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
package asymkey

import (
"errors"
"fmt"
"strings"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/perm"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
)

// __________ .__ .__ .__
Expand Down Expand Up @@ -70,7 +70,7 @@ func CheckPrincipalKeyString(user *user_model.User, content string) (_ string, e

content = strings.TrimSpace(content)
if strings.ContainsAny(content, "\r\n") {
return "", errors.New("only a single line with a single principal please")
return "", util.NewInvalidArgumentErrorf("only a single line with a single principal please")
}

// check all the allowed principals, email, username or anything
Expand Down
5 changes: 2 additions & 3 deletions models/org_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package models

import (
"context"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -235,7 +234,7 @@ func RemoveRepository(t *organization.Team, repoID int64) error {
// It's caller's responsibility to assign organization ID.
func NewTeam(t *organization.Team) (err error) {
if len(t.Name) == 0 {
return errors.New("empty team name")
return util.NewInvalidArgumentErrorf("empty team name")
}

if err = organization.IsUsableTeamName(t.Name); err != nil {
Expand Down Expand Up @@ -300,7 +299,7 @@ func NewTeam(t *organization.Team) (err error) {
// UpdateTeam updates information of team.
func UpdateTeam(t *organization.Team, authChanged, includeAllChanged bool) (err error) {
if len(t.Name) == 0 {
return errors.New("empty team name")
return util.NewInvalidArgumentErrorf("empty team name")
}

if len(t.Description) > 255 {
Expand Down
6 changes: 3 additions & 3 deletions models/packages/conan/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ package conan

import (
"context"
"errors"
"strconv"
"strings"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/packages"
conan_module "code.gitea.io/gitea/modules/packages/conan"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

"xorm.io/builder"
)

var (
ErrRecipeReferenceNotExist = errors.New("Recipe reference does not exist")
ErrPackageReferenceNotExist = errors.New("Package reference does not exist")
ErrRecipeReferenceNotExist = util.NewNotExistErrorf("recipe reference does not exist")
ErrPackageReferenceNotExist = util.NewNotExistErrorf("package reference does not exist")
)

// RecipeExists checks if a recipe exists
Expand Down
4 changes: 2 additions & 2 deletions models/packages/container/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ package container

import (
"context"
"errors"
"strings"
"time"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/packages"
user_model "code.gitea.io/gitea/models/user"
container_module "code.gitea.io/gitea/modules/packages/container"
"code.gitea.io/gitea/modules/util"

"xorm.io/builder"
)

var ErrContainerBlobNotExist = errors.New("Container blob does not exist")
var ErrContainerBlobNotExist = util.NewNotExistErrorf("container blob does not exist")

type BlobSearchOptions struct {
OwnerID int64
Expand Down
6 changes: 3 additions & 3 deletions models/packages/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ package packages

import (
"context"
"errors"
"fmt"
"strings"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/util"

"xorm.io/builder"
)
Expand All @@ -20,9 +20,9 @@ func init() {

var (
// ErrDuplicatePackage indicates a duplicated package error
ErrDuplicatePackage = errors.New("Package does exist already")
ErrDuplicatePackage = util.NewAlreadyExistErrorf("package already exists")
// ErrPackageNotExist indicates a package not exist error
ErrPackageNotExist = errors.New("Package does not exist")
ErrPackageNotExist = util.NewNotExistErrorf("package does not exist")
)

// Type of a package
Expand Down
4 changes: 2 additions & 2 deletions models/packages/package_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ package packages

import (
"context"
"errors"
"time"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
)

// ErrPackageBlobNotExist indicates a package blob not exist error
var ErrPackageBlobNotExist = errors.New("Package blob does not exist")
var ErrPackageBlobNotExist = util.NewNotExistErrorf("package blob does not exist")

func init() {
db.RegisterModel(new(PackageBlob))
Expand Down
3 changes: 1 addition & 2 deletions models/packages/package_blob_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package packages

import (
"context"
"errors"
"strings"
"time"

Expand All @@ -15,7 +14,7 @@ import (
)

// ErrPackageBlobUploadNotExist indicates a package blob upload not exist error
var ErrPackageBlobUploadNotExist = errors.New("Package blob upload does not exist")
var ErrPackageBlobUploadNotExist = util.NewNotExistErrorf("package blob upload does not exist")

func init() {
db.RegisterModel(new(PackageBlobUpload))
Expand Down
4 changes: 2 additions & 2 deletions models/packages/package_cleanup_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ package packages

import (
"context"
"errors"
"fmt"
"regexp"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

"xorm.io/builder"
)

var ErrPackageCleanupRuleNotExist = errors.New("Package blob does not exist")
var ErrPackageCleanupRuleNotExist = util.NewNotExistErrorf("package blob does not exist")

func init() {
db.RegisterModel(new(PackageCleanupRule))
Expand Down
6 changes: 3 additions & 3 deletions models/packages/package_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ package packages

import (
"context"
"errors"
"strconv"
"strings"
"time"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

"xorm.io/builder"
)
Expand All @@ -22,9 +22,9 @@ func init() {

var (
// ErrDuplicatePackageFile indicates a duplicated package file error
ErrDuplicatePackageFile = errors.New("Package file does exist already")
ErrDuplicatePackageFile = util.NewAlreadyExistErrorf("package file already exists")
// ErrPackageFileNotExist indicates a package file not exist error
ErrPackageFileNotExist = errors.New("Package file does not exist")
ErrPackageFileNotExist = util.NewNotExistErrorf("package file does not exist")
)

// EmptyFileKey is a named constant for an empty file key
Expand Down
3 changes: 1 addition & 2 deletions models/packages/package_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package packages

import (
"context"
"errors"
"strconv"
"strings"

Expand All @@ -17,7 +16,7 @@ import (
)

// ErrDuplicatePackageVersion indicates a duplicated package version error
var ErrDuplicatePackageVersion = errors.New("Package version already exists")
var ErrDuplicatePackageVersion = util.NewAlreadyExistErrorf("package version already exists")

func init() {
db.RegisterModel(new(PackageVersion))
Expand Down
3 changes: 1 addition & 2 deletions models/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package project

import (
"context"
"errors"
"fmt"

"code.gitea.io/gitea/models/db"
Expand Down Expand Up @@ -176,7 +175,7 @@ func NewProject(p *Project) error {
}

if !IsTypeValid(p.Type) {
return errors.New("project type is not valid")
return util.NewInvalidArgumentErrorf("project type is not valid")
}

ctx, committer, err := db.TxContext(db.DefaultContext)
Expand Down
4 changes: 2 additions & 2 deletions models/repo/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ package repo

import (
"context"
"errors"
"time"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
)

// ErrMirrorNotExist mirror does not exist error
var ErrMirrorNotExist = errors.New("Mirror does not exist")
var ErrMirrorNotExist = util.NewNotExistErrorf("Mirror does not exist")

// Mirror represents mirror information of a repository.
type Mirror struct {
Expand Down
6 changes: 3 additions & 3 deletions models/repo/pushmirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ package repo

import (
"context"
"errors"
"time"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

"xorm.io/builder"
)

// ErrPushMirrorNotExist mirror does not exist error
var ErrPushMirrorNotExist = errors.New("PushMirror does not exist")
var ErrPushMirrorNotExist = util.NewNotExistErrorf("PushMirror does not exist")

// PushMirror represents mirror information of a repository.
type PushMirror struct {
Expand Down Expand Up @@ -90,7 +90,7 @@ func DeletePushMirrors(ctx context.Context, opts PushMirrorOptions) error {
_, err := db.GetEngine(ctx).Where(opts.toConds()).Delete(&PushMirror{})
return err
}
return errors.New("repoID required and must be set")
return util.NewInvalidArgumentErrorf("repoID required and must be set")
}

func GetPushMirror(ctx context.Context, opts PushMirrorOptions) (*PushMirror, error) {
Expand Down
3 changes: 1 addition & 2 deletions models/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package repo

import (
"context"
"errors"
"fmt"
"sort"
"strconv"
Expand Down Expand Up @@ -156,7 +155,7 @@ func AddReleaseAttachments(ctx context.Context, releaseID int64, attachmentUUIDs

for i := range attachments {
if attachments[i].ReleaseID != 0 {
return errors.New("release permission denied")
return util.NewPermissionDeniedErrorf("release permission denied")
}
attachments[i].ReleaseID = releaseID
// No assign value could be 0, so ignore AllCols().
Expand Down
3 changes: 1 addition & 2 deletions models/repo/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package repo

import (
"context"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -708,7 +707,7 @@ func GetUserRepositories(opts *SearchRepoOptions) (RepositoryList, int64, error)

cond := builder.NewCond()
if opts.Actor == nil {
return nil, 0, errors.New("GetUserRepositories: Actor is needed but not given")
return nil, 0, util.NewInvalidArgumentErrorf("GetUserRepositories: Actor is needed but not given")
}
cond = cond.And(builder.Eq{"owner_id": opts.Actor.ID})
if !opts.Private {
Expand Down
2 changes: 1 addition & 1 deletion models/unittest/fscopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func Copy(src, dest string) error {
func CopyDir(srcPath, destPath string, filters ...func(filePath string) bool) error {
// Check if target directory exists.
if _, err := os.Stat(destPath); !errors.Is(err, os.ErrNotExist) {
return errors.New("file or directory already exists: " + destPath)
return util.NewAlreadyExistErrorf("file or directory already exists: %s", destPath)
}

err := os.MkdirAll(destPath, os.ModePerm)
Expand Down
3 changes: 1 addition & 2 deletions models/user/email_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package user

import (
"context"
"errors"
"fmt"
"net/mail"
"regexp"
Expand All @@ -22,7 +21,7 @@ import (
)

// ErrEmailNotActivated e-mail address has not been activated error
var ErrEmailNotActivated = errors.New("e-mail address has not been activated")
var ErrEmailNotActivated = util.NewInvalidArgumentErrorf("e-mail address has not been activated")

// ErrEmailCharIsNotSupported e-mail address contains unsupported character
type ErrEmailCharIsNotSupported struct {
Expand Down
Loading

0 comments on commit 3fef47b

Please sign in to comment.