From a561f38d4ef67032092ff6f78784c96d37da8229 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Thu, 29 Dec 2022 16:13:02 +0000 Subject: [PATCH 1/2] Use ErrInvalidArgument. --- modules/packages/composer/metadata.go | 11 ++++------- modules/packages/helm/metadata.go | 14 +++++--------- modules/packages/npm/creator.go | 17 ++++++----------- modules/packages/nuget/metadata.go | 14 +++++--------- modules/packages/nuget/symbol_extractor.go | 10 +++++----- modules/packages/pub/metadata.go | 10 +++++----- modules/packages/rubygems/marshal.go | 9 ++++----- modules/packages/rubygems/metadata.go | 11 ++++------- routers/api/packages/composer/composer.go | 7 ++++++- routers/api/packages/helm/helm.go | 7 ++++++- routers/api/packages/npm/npm.go | 6 +++++- routers/api/packages/nuget/nuget.go | 8 ++++++-- routers/api/packages/pub/pub.go | 8 +++++++- routers/api/packages/rubygems/rubygems.go | 7 ++++++- 14 files changed, 74 insertions(+), 65 deletions(-) diff --git a/modules/packages/composer/metadata.go b/modules/packages/composer/metadata.go index b98294001c287..c0e44aad7b2da 100644 --- a/modules/packages/composer/metadata.go +++ b/modules/packages/composer/metadata.go @@ -5,12 +5,12 @@ package composer import ( "archive/zip" - "errors" "io" "regexp" "strings" "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/validation" "github.com/hashicorp/go-version" @@ -20,12 +20,9 @@ import ( const TypeProperty = "composer.type" var ( - // ErrMissingComposerFile indicates a missing composer.json file - ErrMissingComposerFile = errors.New("composer.json file is missing") - // ErrInvalidName indicates an invalid package name - ErrInvalidName = errors.New("package name is invalid") - // ErrInvalidVersion indicates an invalid package version - ErrInvalidVersion = errors.New("package version is invalid") + ErrMissingComposerFile = util.SilentWrap{Message: "composer.json file is missing", Err: util.ErrInvalidArgument} + ErrInvalidName = util.SilentWrap{Message: "package name is invalid", Err: util.ErrInvalidArgument} + ErrInvalidVersion = util.SilentWrap{Message: "package version is invalid", Err: util.ErrInvalidArgument} ) // Package represents a Composer package diff --git a/modules/packages/helm/metadata.go b/modules/packages/helm/metadata.go index 98b5919a73f2e..f7562dcc47303 100644 --- a/modules/packages/helm/metadata.go +++ b/modules/packages/helm/metadata.go @@ -6,10 +6,10 @@ package helm import ( "archive/tar" "compress/gzip" - "errors" "io" "strings" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/validation" "github.com/hashicorp/go-version" @@ -17,14 +17,10 @@ import ( ) var ( - // ErrMissingChartFile indicates a missing Chart.yaml file - ErrMissingChartFile = errors.New("Chart.yaml file is missing") - // ErrInvalidName indicates an invalid package name - ErrInvalidName = errors.New("package name is invalid") - // ErrInvalidVersion indicates an invalid package version - ErrInvalidVersion = errors.New("package version is invalid") - // ErrInvalidChart indicates an invalid chart - ErrInvalidChart = errors.New("chart is invalid") + ErrMissingChartFile = util.SilentWrap{Message: "Chart.yaml file is missing", Err: util.ErrInvalidArgument} + ErrInvalidName = util.SilentWrap{Message: "package name is invalid", Err: util.ErrInvalidArgument} + ErrInvalidVersion = util.SilentWrap{Message: "package version is invalid", Err: util.ErrInvalidArgument} + ErrInvalidChart = util.SilentWrap{Message: "chart is invalid", Err: util.ErrInvalidArgument} ) // Metadata for a Chart file. This models the structure of a Chart.yaml file. diff --git a/modules/packages/npm/creator.go b/modules/packages/npm/creator.go index 02f67248129de..6cfbc9a098a7f 100644 --- a/modules/packages/npm/creator.go +++ b/modules/packages/npm/creator.go @@ -8,7 +8,6 @@ import ( "crypto/sha1" "crypto/sha512" "encoding/base64" - "errors" "fmt" "io" "regexp" @@ -16,22 +15,18 @@ import ( "time" "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/validation" "github.com/hashicorp/go-version" ) var ( - // ErrInvalidPackage indicates an invalid package - ErrInvalidPackage = errors.New("The package is invalid") - // ErrInvalidPackageName indicates an invalid name - ErrInvalidPackageName = errors.New("The package name is invalid") - // ErrInvalidPackageVersion indicates an invalid version - ErrInvalidPackageVersion = errors.New("The package version is invalid") - // ErrInvalidAttachment indicates a invalid attachment - ErrInvalidAttachment = errors.New("The package attachment is invalid") - // ErrInvalidIntegrity indicates an integrity validation error - ErrInvalidIntegrity = errors.New("Failed to validate integrity") + ErrInvalidPackage = util.SilentWrap{Message: "package is invalid", Err: util.ErrInvalidArgument} + ErrInvalidPackageName = util.SilentWrap{Message: "package name is invalid", Err: util.ErrInvalidArgument} + ErrInvalidPackageVersion = util.SilentWrap{Message: "package version is invalid", Err: util.ErrInvalidArgument} + ErrInvalidAttachment = util.SilentWrap{Message: "package attachment is invalid", Err: util.ErrInvalidArgument} + ErrInvalidIntegrity = util.SilentWrap{Message: "failed to validate integrity", Err: util.ErrInvalidArgument} ) var nameMatch = regexp.MustCompile(`\A((@[^\s\/~'!\(\)\*]+?)[\/])?([^_.][^\s\/~'!\(\)\*]+)\z`) diff --git a/modules/packages/nuget/metadata.go b/modules/packages/nuget/metadata.go index 033421af96a86..99402ba9cf6ca 100644 --- a/modules/packages/nuget/metadata.go +++ b/modules/packages/nuget/metadata.go @@ -7,27 +7,23 @@ import ( "archive/zip" "bytes" "encoding/xml" - "errors" "fmt" "io" "path/filepath" "regexp" "strings" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/validation" "github.com/hashicorp/go-version" ) var ( - // ErrMissingNuspecFile indicates a missing Nuspec file - ErrMissingNuspecFile = errors.New("Nuspec file is missing") - // ErrNuspecFileTooLarge indicates a Nuspec file which is too large - ErrNuspecFileTooLarge = errors.New("Nuspec file is too large") - // ErrNuspecInvalidID indicates an invalid id in the Nuspec file - ErrNuspecInvalidID = errors.New("Nuspec file contains an invalid id") - // ErrNuspecInvalidVersion indicates an invalid version in the Nuspec file - ErrNuspecInvalidVersion = errors.New("Nuspec file contains an invalid version") + ErrMissingNuspecFile = util.SilentWrap{Message: "Nuspec file is missing", Err: util.ErrInvalidArgument} + ErrNuspecFileTooLarge = util.SilentWrap{Message: "Nuspec file is too large", Err: util.ErrInvalidArgument} + ErrNuspecInvalidID = util.SilentWrap{Message: "Nuspec file contains an invalid id", Err: util.ErrInvalidArgument} + ErrNuspecInvalidVersion = util.SilentWrap{Message: "Nuspec file contains an invalid version", Err: util.ErrInvalidArgument} ) // PackageType specifies the package type the metadata describes diff --git a/modules/packages/nuget/symbol_extractor.go b/modules/packages/nuget/symbol_extractor.go index 634bbb17e6214..79fcc2b6ca9f9 100644 --- a/modules/packages/nuget/symbol_extractor.go +++ b/modules/packages/nuget/symbol_extractor.go @@ -7,7 +7,6 @@ import ( "archive/zip" "bytes" "encoding/binary" - "errors" "fmt" "io" "path" @@ -15,13 +14,14 @@ import ( "strings" "code.gitea.io/gitea/modules/packages" + "code.gitea.io/gitea/modules/util" ) var ( - ErrMissingPdbFiles = errors.New("Package does not contain PDB files") - ErrInvalidFiles = errors.New("Package contains invalid files") - ErrInvalidPdbMagicNumber = errors.New("Invalid Portable PDB magic number") - ErrMissingPdbStream = errors.New("Missing PDB stream") + ErrMissingPdbFiles = util.SilentWrap{Message: "package does not contain PDB files", Err: util.ErrInvalidArgument} + ErrInvalidFiles = util.SilentWrap{Message: "package contains invalid files", Err: util.ErrInvalidArgument} + ErrInvalidPdbMagicNumber = util.SilentWrap{Message: "invalid Portable PDB magic number", Err: util.ErrInvalidArgument} + ErrMissingPdbStream = util.SilentWrap{Message: "missing PDB stream", Err: util.ErrInvalidArgument} ) type PortablePdb struct { diff --git a/modules/packages/pub/metadata.go b/modules/packages/pub/metadata.go index 36fe665707695..9be035c5f7d7e 100644 --- a/modules/packages/pub/metadata.go +++ b/modules/packages/pub/metadata.go @@ -6,11 +6,11 @@ package pub import ( "archive/tar" "compress/gzip" - "errors" "io" "regexp" "strings" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/validation" "github.com/hashicorp/go-version" @@ -18,10 +18,10 @@ import ( ) var ( - ErrMissingPubspecFile = errors.New("Pubspec file is missing") - ErrPubspecFileTooLarge = errors.New("Pubspec file is too large") - ErrInvalidName = errors.New("Package name is invalid") - ErrInvalidVersion = errors.New("Package version is invalid") + ErrMissingPubspecFile = util.SilentWrap{Message: "Pubspec file is missing", Err: util.ErrInvalidArgument} + ErrPubspecFileTooLarge = util.SilentWrap{Message: "Pubspec file is too large", Err: util.ErrInvalidArgument} + ErrInvalidName = util.SilentWrap{Message: "Package name is invalid", Err: util.ErrInvalidArgument} + ErrInvalidVersion = util.SilentWrap{Message: "Package version is invalid", Err: util.ErrInvalidArgument} ) var namePattern = regexp.MustCompile(`\A[a-zA-Z_][a-zA-Z0-9_]*\z`) diff --git a/modules/packages/rubygems/marshal.go b/modules/packages/rubygems/marshal.go index 14fb755606e97..2edf825e8d5c2 100644 --- a/modules/packages/rubygems/marshal.go +++ b/modules/packages/rubygems/marshal.go @@ -6,9 +6,10 @@ package rubygems import ( "bufio" "bytes" - "errors" "io" "reflect" + + "code.gitea.io/gitea/modules/util" ) const ( @@ -30,10 +31,8 @@ const ( ) var ( - // ErrUnsupportedType indicates an unsupported type - ErrUnsupportedType = errors.New("Type is unsupported") - // ErrInvalidIntRange indicates an invalid number range - ErrInvalidIntRange = errors.New("Number is not in valid range") + ErrUnsupportedType = util.SilentWrap{Message: "type is unsupported", Err: util.ErrInvalidArgument} + ErrInvalidIntRange = util.SilentWrap{Message: "number is not in valid range", Err: util.ErrInvalidArgument} ) // RubyUserMarshal is a Ruby object that has a marshal_load function. diff --git a/modules/packages/rubygems/metadata.go b/modules/packages/rubygems/metadata.go index e2c73c8f3a0de..ee7d39e2a4d40 100644 --- a/modules/packages/rubygems/metadata.go +++ b/modules/packages/rubygems/metadata.go @@ -6,23 +6,20 @@ package rubygems import ( "archive/tar" "compress/gzip" - "errors" "io" "regexp" "strings" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/validation" "gopkg.in/yaml.v3" ) var ( - // ErrMissingMetadataFile indicates a missing metadata.gz file - ErrMissingMetadataFile = errors.New("Metadata file is missing") - // ErrInvalidName indicates an invalid id in the metadata.gz file - ErrInvalidName = errors.New("Metadata file contains an invalid name") - // ErrInvalidVersion indicates an invalid version in the metadata.gz file - ErrInvalidVersion = errors.New("Metadata file contains an invalid version") + ErrMissingMetadataFile = util.SilentWrap{Message: "metadata.gz file is missing", Err: util.ErrInvalidArgument} + ErrInvalidName = util.SilentWrap{Message: "package name is invalid", Err: util.ErrInvalidArgument} + ErrInvalidVersion = util.SilentWrap{Message: "package version is invalid", Err: util.ErrInvalidArgument} ) var versionMatcher = regexp.MustCompile(`\A[0-9]+(?:\.[0-9a-zA-Z]+)*(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?\z`) diff --git a/routers/api/packages/composer/composer.go b/routers/api/packages/composer/composer.go index c6e0593b42f5e..2797293d06d33 100644 --- a/routers/api/packages/composer/composer.go +++ b/routers/api/packages/composer/composer.go @@ -4,6 +4,7 @@ package composer import ( + "errors" "fmt" "io" "net/http" @@ -200,7 +201,11 @@ func UploadPackage(ctx *context.Context) { cp, err := composer_module.ParsePackage(buf, buf.Size()) if err != nil { - apiError(ctx, http.StatusBadRequest, err) + if errors.Is(err, util.ErrInvalidArgument) { + apiError(ctx, http.StatusBadRequest, err) + } else { + apiError(ctx, http.StatusInternalServerError, err) + } return } diff --git a/routers/api/packages/helm/helm.go b/routers/api/packages/helm/helm.go index af863bc4b91d0..3bcce6bdf5855 100644 --- a/routers/api/packages/helm/helm.go +++ b/routers/api/packages/helm/helm.go @@ -4,6 +4,7 @@ package helm import ( + "errors" "fmt" "io" "net/http" @@ -163,7 +164,11 @@ func UploadPackage(ctx *context.Context) { metadata, err := helm_module.ParseChartArchive(buf) if err != nil { - apiError(ctx, http.StatusBadRequest, err) + if errors.Is(err, util.ErrInvalidArgument) { + apiError(ctx, http.StatusBadRequest, err) + } else { + apiError(ctx, http.StatusInternalServerError, err) + } return } diff --git a/routers/api/packages/npm/npm.go b/routers/api/packages/npm/npm.go index 1a09cb6f3663b..0d25f173e922b 100644 --- a/routers/api/packages/npm/npm.go +++ b/routers/api/packages/npm/npm.go @@ -158,7 +158,11 @@ func DownloadPackageFileByName(ctx *context.Context) { func UploadPackage(ctx *context.Context) { npmPackage, err := npm_module.ParsePackage(ctx.Req.Body) if err != nil { - apiError(ctx, http.StatusBadRequest, err) + if errors.Is(err, util.ErrInvalidArgument) { + apiError(ctx, http.StatusBadRequest, err) + } else { + apiError(ctx, http.StatusInternalServerError, err) + } return } diff --git a/routers/api/packages/nuget/nuget.go b/routers/api/packages/nuget/nuget.go index d6f7d1d7f66ca..6423db7d3a214 100644 --- a/routers/api/packages/nuget/nuget.go +++ b/routers/api/packages/nuget/nuget.go @@ -411,7 +411,11 @@ func UploadSymbolPackage(ctx *context.Context) { pdbs, err := nuget_module.ExtractPortablePdb(buf, buf.Size()) if err != nil { - apiError(ctx, http.StatusBadRequest, err) + if errors.Is(err, util.ErrInvalidArgument) { + apiError(ctx, http.StatusBadRequest, err) + } else { + apiError(ctx, http.StatusInternalServerError, err) + } return } defer pdbs.Close() @@ -507,7 +511,7 @@ func processUploadedFile(ctx *context.Context, expectedType nuget_module.Package np, err := nuget_module.ParsePackageMetaData(buf, buf.Size()) if err != nil { - if err == nuget_module.ErrMissingNuspecFile || err == nuget_module.ErrNuspecFileTooLarge || err == nuget_module.ErrNuspecInvalidID || err == nuget_module.ErrNuspecInvalidVersion { + if errors.Is(err, util.ErrInvalidArgument) { apiError(ctx, http.StatusBadRequest, err) } else { apiError(ctx, http.StatusInternalServerError, err) diff --git a/routers/api/packages/pub/pub.go b/routers/api/packages/pub/pub.go index 247950a214ae9..1ece4e18ed6d4 100644 --- a/routers/api/packages/pub/pub.go +++ b/routers/api/packages/pub/pub.go @@ -4,6 +4,7 @@ package pub import ( + "errors" "fmt" "io" "net/http" @@ -19,6 +20,7 @@ import ( packages_module "code.gitea.io/gitea/modules/packages" pub_module "code.gitea.io/gitea/modules/packages/pub" "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" ) @@ -173,7 +175,11 @@ func UploadPackageFile(ctx *context.Context) { pck, err := pub_module.ParsePackage(buf) if err != nil { - apiError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, util.ErrInvalidArgument) { + apiError(ctx, http.StatusBadRequest, err) + } else { + apiError(ctx, http.StatusInternalServerError, err) + } return } diff --git a/routers/api/packages/rubygems/rubygems.go b/routers/api/packages/rubygems/rubygems.go index c1a10b5e78f34..af358fb82fbad 100644 --- a/routers/api/packages/rubygems/rubygems.go +++ b/routers/api/packages/rubygems/rubygems.go @@ -6,6 +6,7 @@ package rubygems import ( "compress/gzip" "compress/zlib" + "errors" "fmt" "io" "net/http" @@ -217,7 +218,11 @@ func UploadPackageFile(ctx *context.Context) { rp, err := rubygems_module.ParsePackageMetaData(buf) if err != nil { - apiError(ctx, http.StatusInternalServerError, err) + if errors.Is(err, util.ErrInvalidArgument) { + apiError(ctx, http.StatusBadRequest, err) + } else { + apiError(ctx, http.StatusInternalServerError, err) + } return } if _, err := buf.Seek(0, io.SeekStart); err != nil { From a6dca9399335605b9f0f6e4fc820f65f52227f8e Mon Sep 17 00:00:00 2001 From: zeripath Date: Fri, 30 Dec 2022 11:35:05 +0000 Subject: [PATCH 2/2] Add convenience functions and do some more refactoring (#4) * Add convenience functions and do some more refactoring Signed-off-by: Andrew Thornton * Apply suggestions from code review Signed-off-by: Andrew Thornton Co-authored-by: KN4CK3R --- models/asymkey/ssh_key_fingerprint.go | 5 ++-- models/asymkey/ssh_key_parse.go | 5 ++-- models/asymkey/ssh_key_principals.go | 4 +-- models/org_team.go | 5 ++-- models/packages/conan/references.go | 6 ++--- models/packages/container/search.go | 4 +-- models/packages/package.go | 6 ++--- models/packages/package_blob.go | 4 +-- models/packages/package_blob_upload.go | 3 +-- models/packages/package_cleanup_rule.go | 4 +-- models/packages/package_file.go | 6 ++--- models/packages/package_version.go | 3 +-- models/project/project.go | 3 +-- models/repo/mirror.go | 4 +-- models/repo/pushmirror.go | 6 ++--- models/repo/release.go | 3 +-- models/repo/repo_list.go | 3 +-- models/unittest/fscopy.go | 2 +- models/user/email_address.go | 3 +-- models/user/openid.go | 3 +-- modules/packages/composer/metadata.go | 9 ++++--- modules/packages/conan/conaninfo_parser.go | 5 ++-- modules/packages/conan/reference.go | 4 +-- modules/packages/helm/metadata.go | 12 ++++++--- modules/packages/npm/creator.go | 15 +++++++---- modules/packages/nuget/metadata.go | 12 ++++++--- modules/packages/nuget/symbol_extractor.go | 8 +++--- modules/packages/pub/metadata.go | 8 +++--- modules/packages/rubygems/marshal.go | 6 +++-- modules/packages/rubygems/metadata.go | 9 ++++--- modules/util/error.go | 29 ++++++++++++++++++++++ 31 files changed, 120 insertions(+), 79 deletions(-) diff --git a/models/asymkey/ssh_key_fingerprint.go b/models/asymkey/ssh_key_fingerprint.go index ca0334cc0b1aa..8a8d4fce153d0 100644 --- a/models/asymkey/ssh_key_fingerprint.go +++ b/models/asymkey/ssh_key_fingerprint.go @@ -5,7 +5,6 @@ package asymkey import ( "context" - "errors" "fmt" "strings" @@ -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 } diff --git a/models/asymkey/ssh_key_parse.go b/models/asymkey/ssh_key_parse.go index b3eecb2c9bfe0..1df6db6fa7219 100644 --- a/models/asymkey/ssh_key_parse.go +++ b/models/asymkey/ssh_key_parse.go @@ -10,7 +10,6 @@ import ( "encoding/base64" "encoding/binary" "encoding/pem" - "errors" "fmt" "math/big" "os" @@ -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: @@ -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 diff --git a/models/asymkey/ssh_key_principals.go b/models/asymkey/ssh_key_principals.go index f00c3f3e9e675..6d43437ec106a 100644 --- a/models/asymkey/ssh_key_principals.go +++ b/models/asymkey/ssh_key_principals.go @@ -4,7 +4,6 @@ package asymkey import ( - "errors" "fmt" "strings" @@ -12,6 +11,7 @@ import ( "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" ) // __________ .__ .__ .__ @@ -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 diff --git a/models/org_team.go b/models/org_team.go index a5a2575eec180..b3ee842c1f297 100644 --- a/models/org_team.go +++ b/models/org_team.go @@ -6,7 +6,6 @@ package models import ( "context" - "errors" "fmt" "strings" @@ -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 { @@ -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 { diff --git a/models/packages/conan/references.go b/models/packages/conan/references.go index 06e828e8fa476..0d888a1ec88cb 100644 --- a/models/packages/conan/references.go +++ b/models/packages/conan/references.go @@ -5,7 +5,6 @@ package conan import ( "context" - "errors" "strconv" "strings" @@ -13,13 +12,14 @@ import ( "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 diff --git a/models/packages/container/search.go b/models/packages/container/search.go index dfd5e244ba28b..2e35c44766ae3 100644 --- a/models/packages/container/search.go +++ b/models/packages/container/search.go @@ -5,7 +5,6 @@ package container import ( "context" - "errors" "strings" "time" @@ -13,11 +12,12 @@ import ( "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 diff --git a/models/packages/package.go b/models/packages/package.go index 5c4837d98b0c8..a804f35de3526 100644 --- a/models/packages/package.go +++ b/models/packages/package.go @@ -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" ) @@ -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 diff --git a/models/packages/package_blob.go b/models/packages/package_blob.go index 36ad745312d26..3b4a1ecf1859a 100644 --- a/models/packages/package_blob.go +++ b/models/packages/package_blob.go @@ -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)) diff --git a/models/packages/package_blob_upload.go b/models/packages/package_blob_upload.go index 64d1f9d473db1..4b0e789221bd2 100644 --- a/models/packages/package_blob_upload.go +++ b/models/packages/package_blob_upload.go @@ -5,7 +5,6 @@ package packages import ( "context" - "errors" "strings" "time" @@ -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)) diff --git a/models/packages/package_cleanup_rule.go b/models/packages/package_cleanup_rule.go index 9bd512755de51..fa12dec40602c 100644 --- a/models/packages/package_cleanup_rule.go +++ b/models/packages/package_cleanup_rule.go @@ -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)) diff --git a/models/packages/package_file.go b/models/packages/package_file.go index 6d0fd185a04eb..7f794836dc94e 100644 --- a/models/packages/package_file.go +++ b/models/packages/package_file.go @@ -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" ) @@ -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 diff --git a/models/packages/package_version.go b/models/packages/package_version.go index 928f9d47d6cdc..759c20abed22b 100644 --- a/models/packages/package_version.go +++ b/models/packages/package_version.go @@ -5,7 +5,6 @@ package packages import ( "context" - "errors" "strconv" "strings" @@ -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)) diff --git a/models/project/project.go b/models/project/project.go index bcf1166408f85..0a07cfe22ad1b 100644 --- a/models/project/project.go +++ b/models/project/project.go @@ -5,7 +5,6 @@ package project import ( "context" - "errors" "fmt" "code.gitea.io/gitea/models/db" @@ -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) diff --git a/models/repo/mirror.go b/models/repo/mirror.go index 2f59b85331d0b..c1d24a4886375 100644 --- a/models/repo/mirror.go +++ b/models/repo/mirror.go @@ -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 { diff --git a/models/repo/pushmirror.go b/models/repo/pushmirror.go index f79ce59ee2270..642020bb5e6d8 100644 --- a/models/repo/pushmirror.go +++ b/models/repo/pushmirror.go @@ -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 { @@ -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) { diff --git a/models/repo/release.go b/models/repo/release.go index 25eba10e05dff..08b429f5e1ec6 100644 --- a/models/repo/release.go +++ b/models/repo/release.go @@ -6,7 +6,6 @@ package repo import ( "context" - "errors" "fmt" "sort" "strconv" @@ -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(). diff --git a/models/repo/repo_list.go b/models/repo/repo_list.go index 6d9dd9ec65ac4..9922ff25a2f0e 100644 --- a/models/repo/repo_list.go +++ b/models/repo/repo_list.go @@ -5,7 +5,6 @@ package repo import ( "context" - "errors" "fmt" "strings" @@ -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 { diff --git a/models/unittest/fscopy.go b/models/unittest/fscopy.go index 631da49f2d5b5..74b12d5057791 100644 --- a/models/unittest/fscopy.go +++ b/models/unittest/fscopy.go @@ -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) diff --git a/models/user/email_address.go b/models/user/email_address.go index 69e94f8bb6bee..e310858f92ee9 100644 --- a/models/user/email_address.go +++ b/models/user/email_address.go @@ -6,7 +6,6 @@ package user import ( "context" - "errors" "fmt" "net/mail" "regexp" @@ -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 { diff --git a/models/user/openid.go b/models/user/openid.go index 194670504896b..596ff182bc751 100644 --- a/models/user/openid.go +++ b/models/user/openid.go @@ -5,7 +5,6 @@ package user import ( "context" - "errors" "fmt" "code.gitea.io/gitea/models/db" @@ -13,7 +12,7 @@ import ( ) // ErrOpenIDNotExist openid is not known -var ErrOpenIDNotExist = errors.New("OpenID is unknown") +var ErrOpenIDNotExist = util.NewNotExistErrorf("OpenID is unknown") // UserOpenID is the list of all OpenID identities of a user. // Since this is a middle table, name it OpenID is not suitable, so we ignore the lint here diff --git a/modules/packages/composer/metadata.go b/modules/packages/composer/metadata.go index c0e44aad7b2da..36b0b8e421974 100644 --- a/modules/packages/composer/metadata.go +++ b/modules/packages/composer/metadata.go @@ -20,9 +20,12 @@ import ( const TypeProperty = "composer.type" var ( - ErrMissingComposerFile = util.SilentWrap{Message: "composer.json file is missing", Err: util.ErrInvalidArgument} - ErrInvalidName = util.SilentWrap{Message: "package name is invalid", Err: util.ErrInvalidArgument} - ErrInvalidVersion = util.SilentWrap{Message: "package version is invalid", Err: util.ErrInvalidArgument} + // ErrMissingComposerFile indicates a missing composer.json file + ErrMissingComposerFile = util.NewInvalidArgumentErrorf("composer.json file is missing") + // ErrInvalidName indicates an invalid package name + ErrInvalidName = util.NewInvalidArgumentErrorf("package name is invalid") + // ErrInvalidVersion indicates an invalid package version + ErrInvalidVersion = util.NewInvalidArgumentErrorf("package version is invalid") ) // Package represents a Composer package diff --git a/modules/packages/conan/conaninfo_parser.go b/modules/packages/conan/conaninfo_parser.go index 5bb3fb8930c9e..de11dbee45bf8 100644 --- a/modules/packages/conan/conaninfo_parser.go +++ b/modules/packages/conan/conaninfo_parser.go @@ -5,9 +5,10 @@ package conan import ( "bufio" - "errors" "io" "strings" + + "code.gitea.io/gitea/modules/util" ) // Conaninfo represents infos of a Conan package @@ -79,7 +80,7 @@ func readSections(r io.Reader) (map[string][]string, error) { continue } if line != "" { - return nil, errors.New("Invalid conaninfo.txt") + return nil, util.NewInvalidArgumentErrorf("invalid conaninfo.txt") } } if err := scanner.Err(); err != nil { diff --git a/modules/packages/conan/reference.go b/modules/packages/conan/reference.go index 37a5170dd3b8b..58f268bd48277 100644 --- a/modules/packages/conan/reference.go +++ b/modules/packages/conan/reference.go @@ -4,12 +4,12 @@ package conan import ( - "errors" "fmt" "regexp" "strings" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/util" ) const ( @@ -25,7 +25,7 @@ var ( namePattern = regexp.MustCompile(fmt.Sprintf(`^[a-zA-Z0-9_][a-zA-Z0-9_\+\.-]{%d,%d}$`, minChars-1, maxChars-1)) revisionPattern = regexp.MustCompile(fmt.Sprintf(`^[a-zA-Z0-9]{1,%d}$`, maxChars)) - ErrValidation = errors.New("Could not validate one or more reference fields") + ErrValidation = util.NewInvalidArgumentErrorf("could not validate one or more reference fields") ) // RecipeReference represents a recipe /@/# diff --git a/modules/packages/helm/metadata.go b/modules/packages/helm/metadata.go index f7562dcc47303..fdbd9003b84c2 100644 --- a/modules/packages/helm/metadata.go +++ b/modules/packages/helm/metadata.go @@ -17,10 +17,14 @@ import ( ) var ( - ErrMissingChartFile = util.SilentWrap{Message: "Chart.yaml file is missing", Err: util.ErrInvalidArgument} - ErrInvalidName = util.SilentWrap{Message: "package name is invalid", Err: util.ErrInvalidArgument} - ErrInvalidVersion = util.SilentWrap{Message: "package version is invalid", Err: util.ErrInvalidArgument} - ErrInvalidChart = util.SilentWrap{Message: "chart is invalid", Err: util.ErrInvalidArgument} + // ErrMissingChartFile indicates a missing Chart.yaml file + ErrMissingChartFile = util.NewInvalidArgumentErrorf("Chart.yaml file is missing") + // ErrInvalidName indicates an invalid package name + ErrInvalidName = util.NewInvalidArgumentErrorf("package name is invalid") + // ErrInvalidVersion indicates an invalid package version + ErrInvalidVersion = util.NewInvalidArgumentErrorf("package version is invalid") + // ErrInvalidChart indicates an invalid chart + ErrInvalidChart = util.NewInvalidArgumentErrorf("chart is invalid") ) // Metadata for a Chart file. This models the structure of a Chart.yaml file. diff --git a/modules/packages/npm/creator.go b/modules/packages/npm/creator.go index 6cfbc9a098a7f..548d7ed9e56fa 100644 --- a/modules/packages/npm/creator.go +++ b/modules/packages/npm/creator.go @@ -22,11 +22,16 @@ import ( ) var ( - ErrInvalidPackage = util.SilentWrap{Message: "package is invalid", Err: util.ErrInvalidArgument} - ErrInvalidPackageName = util.SilentWrap{Message: "package name is invalid", Err: util.ErrInvalidArgument} - ErrInvalidPackageVersion = util.SilentWrap{Message: "package version is invalid", Err: util.ErrInvalidArgument} - ErrInvalidAttachment = util.SilentWrap{Message: "package attachment is invalid", Err: util.ErrInvalidArgument} - ErrInvalidIntegrity = util.SilentWrap{Message: "failed to validate integrity", Err: util.ErrInvalidArgument} + // ErrInvalidPackage indicates an invalid package + ErrInvalidPackage = util.NewInvalidArgumentErrorf("package is invalid") + // ErrInvalidPackageName indicates an invalid name + ErrInvalidPackageName = util.NewInvalidArgumentErrorf("package name is invalid") + // ErrInvalidPackageVersion indicates an invalid version + ErrInvalidPackageVersion = util.NewInvalidArgumentErrorf("package version is invalid") + // ErrInvalidAttachment indicates a invalid attachment + ErrInvalidAttachment = util.NewInvalidArgumentErrorf("package attachment is invalid") + // ErrInvalidIntegrity indicates an integrity validation error + ErrInvalidIntegrity = util.NewInvalidArgumentErrorf("failed to validate integrity") ) var nameMatch = regexp.MustCompile(`\A((@[^\s\/~'!\(\)\*]+?)[\/])?([^_.][^\s\/~'!\(\)\*]+)\z`) diff --git a/modules/packages/nuget/metadata.go b/modules/packages/nuget/metadata.go index 99402ba9cf6ca..3c478b1c0288c 100644 --- a/modules/packages/nuget/metadata.go +++ b/modules/packages/nuget/metadata.go @@ -20,10 +20,14 @@ import ( ) var ( - ErrMissingNuspecFile = util.SilentWrap{Message: "Nuspec file is missing", Err: util.ErrInvalidArgument} - ErrNuspecFileTooLarge = util.SilentWrap{Message: "Nuspec file is too large", Err: util.ErrInvalidArgument} - ErrNuspecInvalidID = util.SilentWrap{Message: "Nuspec file contains an invalid id", Err: util.ErrInvalidArgument} - ErrNuspecInvalidVersion = util.SilentWrap{Message: "Nuspec file contains an invalid version", Err: util.ErrInvalidArgument} + // ErrMissingNuspecFile indicates a missing Nuspec file + ErrMissingNuspecFile = util.NewInvalidArgumentErrorf("Nuspec file is missing") + // ErrNuspecFileTooLarge indicates a Nuspec file which is too large + ErrNuspecFileTooLarge = util.NewInvalidArgumentErrorf("Nuspec file is too large") + // ErrNuspecInvalidID indicates an invalid id in the Nuspec file + ErrNuspecInvalidID = util.NewInvalidArgumentErrorf("Nuspec file contains an invalid id") + // ErrNuspecInvalidVersion indicates an invalid version in the Nuspec file + ErrNuspecInvalidVersion = util.NewInvalidArgumentErrorf("Nuspec file contains an invalid version") ) // PackageType specifies the package type the metadata describes diff --git a/modules/packages/nuget/symbol_extractor.go b/modules/packages/nuget/symbol_extractor.go index 79fcc2b6ca9f9..b709eac4c1965 100644 --- a/modules/packages/nuget/symbol_extractor.go +++ b/modules/packages/nuget/symbol_extractor.go @@ -18,10 +18,10 @@ import ( ) var ( - ErrMissingPdbFiles = util.SilentWrap{Message: "package does not contain PDB files", Err: util.ErrInvalidArgument} - ErrInvalidFiles = util.SilentWrap{Message: "package contains invalid files", Err: util.ErrInvalidArgument} - ErrInvalidPdbMagicNumber = util.SilentWrap{Message: "invalid Portable PDB magic number", Err: util.ErrInvalidArgument} - ErrMissingPdbStream = util.SilentWrap{Message: "missing PDB stream", Err: util.ErrInvalidArgument} + ErrMissingPdbFiles = util.NewInvalidArgumentErrorf("package does not contain PDB files") + ErrInvalidFiles = util.NewInvalidArgumentErrorf("package contains invalid files") + ErrInvalidPdbMagicNumber = util.NewInvalidArgumentErrorf("invalid Portable PDB magic number") + ErrMissingPdbStream = util.NewInvalidArgumentErrorf("missing PDB stream") ) type PortablePdb struct { diff --git a/modules/packages/pub/metadata.go b/modules/packages/pub/metadata.go index 9be035c5f7d7e..13a066afacc9d 100644 --- a/modules/packages/pub/metadata.go +++ b/modules/packages/pub/metadata.go @@ -18,10 +18,10 @@ import ( ) var ( - ErrMissingPubspecFile = util.SilentWrap{Message: "Pubspec file is missing", Err: util.ErrInvalidArgument} - ErrPubspecFileTooLarge = util.SilentWrap{Message: "Pubspec file is too large", Err: util.ErrInvalidArgument} - ErrInvalidName = util.SilentWrap{Message: "Package name is invalid", Err: util.ErrInvalidArgument} - ErrInvalidVersion = util.SilentWrap{Message: "Package version is invalid", Err: util.ErrInvalidArgument} + ErrMissingPubspecFile = util.NewInvalidArgumentErrorf("Pubspec file is missing") + ErrPubspecFileTooLarge = util.NewInvalidArgumentErrorf("Pubspec file is too large") + ErrInvalidName = util.NewInvalidArgumentErrorf("package name is invalid") + ErrInvalidVersion = util.NewInvalidArgumentErrorf("package version is invalid") ) var namePattern = regexp.MustCompile(`\A[a-zA-Z_][a-zA-Z0-9_]*\z`) diff --git a/modules/packages/rubygems/marshal.go b/modules/packages/rubygems/marshal.go index 2edf825e8d5c2..efb2ba34a4921 100644 --- a/modules/packages/rubygems/marshal.go +++ b/modules/packages/rubygems/marshal.go @@ -31,8 +31,10 @@ const ( ) var ( - ErrUnsupportedType = util.SilentWrap{Message: "type is unsupported", Err: util.ErrInvalidArgument} - ErrInvalidIntRange = util.SilentWrap{Message: "number is not in valid range", Err: util.ErrInvalidArgument} + // ErrUnsupportedType indicates an unsupported type + ErrUnsupportedType = util.NewInvalidArgumentErrorf("type is unsupported") + // ErrInvalidIntRange indicates an invalid number range + ErrInvalidIntRange = util.NewInvalidArgumentErrorf("number is not in valid range") ) // RubyUserMarshal is a Ruby object that has a marshal_load function. diff --git a/modules/packages/rubygems/metadata.go b/modules/packages/rubygems/metadata.go index ee7d39e2a4d40..adc1c05808740 100644 --- a/modules/packages/rubygems/metadata.go +++ b/modules/packages/rubygems/metadata.go @@ -17,9 +17,12 @@ import ( ) var ( - ErrMissingMetadataFile = util.SilentWrap{Message: "metadata.gz file is missing", Err: util.ErrInvalidArgument} - ErrInvalidName = util.SilentWrap{Message: "package name is invalid", Err: util.ErrInvalidArgument} - ErrInvalidVersion = util.SilentWrap{Message: "package version is invalid", Err: util.ErrInvalidArgument} + // ErrMissingMetadataFile indicates a missing metadata.gz file + ErrMissingMetadataFile = util.NewInvalidArgumentErrorf("metadata.gz file is missing") + // ErrInvalidName indicates an invalid id in the metadata.gz file + ErrInvalidName = util.NewInvalidArgumentErrorf("package name is invalid") + // ErrInvalidVersion indicates an invalid version in the metadata.gz file + ErrInvalidVersion = util.NewInvalidArgumentErrorf("package version is invalid") ) var versionMatcher = regexp.MustCompile(`\A[0-9]+(?:\.[0-9a-zA-Z]+)*(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?\z`) diff --git a/modules/util/error.go b/modules/util/error.go index 63bd447bf3e6d..e67b9977f080c 100644 --- a/modules/util/error.go +++ b/modules/util/error.go @@ -5,6 +5,7 @@ package util import ( "errors" + "fmt" ) // Common Errors forming the base of our error system @@ -34,3 +35,31 @@ func (w SilentWrap) Error() string { func (w SilentWrap) Unwrap() error { return w.Err } + +// NewSilentWrapErrorf returns an error that formats as the given text but unwraps as the provided error +func NewSilentWrapErrorf(unwrap error, message string, args ...interface{}) error { + if len(args) == 0 { + return SilentWrap{Message: message, Err: unwrap} + } + return SilentWrap{Message: fmt.Sprintf(message, args...), Err: unwrap} +} + +// NewInvalidArgumentErrorf returns an error that formats as the given text but unwraps as an ErrInvalidArgument +func NewInvalidArgumentErrorf(message string, args ...interface{}) error { + return NewSilentWrapErrorf(ErrInvalidArgument, message, args...) +} + +// NewPermissionDeniedErrorf returns an error that formats as the given text but unwraps as an ErrPermissionDenied +func NewPermissionDeniedErrorf(message string, args ...interface{}) error { + return NewSilentWrapErrorf(ErrPermissionDenied, message, args...) +} + +// NewAlreadyExistErrorf returns an error that formats as the given text but unwraps as an ErrAlreadyExist +func NewAlreadyExistErrorf(message string, args ...interface{}) error { + return NewSilentWrapErrorf(ErrAlreadyExist, message, args...) +} + +// NewNotExistErrorf returns an error that formats as the given text but unwraps as an ErrNotExist +func NewNotExistErrorf(message string, args ...interface{}) error { + return NewSilentWrapErrorf(ErrNotExist, message, args...) +}