diff options
author | KN4CK3R <admin@oldschoolhack.me> | 2022-12-31 12:49:37 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-31 12:49:37 +0100 |
commit | 3fef47b41c98392865d13fd21bbcec34236daf4f (patch) | |
tree | 16808c6ea9ffd2cc0438d11831fd3a7503d15056 /modules | |
parent | dce8887494268c99ba5da8d5152b5d1cc03cfb83 (diff) | |
download | gitea-3fef47b41c98392865d13fd21bbcec34236daf4f.tar.gz gitea-3fef47b41c98392865d13fd21bbcec34236daf4f.zip |
Use ErrInvalidArgument in packages (#22268)
Related to
https://github.com/go-gitea/gitea/pull/22262#discussion_r1059010774
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>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/packages/composer/metadata.go | 8 | ||||
-rw-r--r-- | modules/packages/conan/conaninfo_parser.go | 5 | ||||
-rw-r--r-- | modules/packages/conan/reference.go | 4 | ||||
-rw-r--r-- | modules/packages/helm/metadata.go | 10 | ||||
-rw-r--r-- | modules/packages/npm/creator.go | 12 | ||||
-rw-r--r-- | modules/packages/nuget/metadata.go | 10 | ||||
-rw-r--r-- | modules/packages/nuget/symbol_extractor.go | 10 | ||||
-rw-r--r-- | modules/packages/pub/metadata.go | 10 | ||||
-rw-r--r-- | modules/packages/rubygems/marshal.go | 7 | ||||
-rw-r--r-- | modules/packages/rubygems/metadata.go | 8 | ||||
-rw-r--r-- | modules/util/error.go | 29 |
11 files changed, 72 insertions, 41 deletions
diff --git a/modules/packages/composer/metadata.go b/modules/packages/composer/metadata.go index b98294001c..36b0b8e421 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" @@ -21,11 +21,11 @@ const TypeProperty = "composer.type" var ( // ErrMissingComposerFile indicates a missing composer.json file - ErrMissingComposerFile = errors.New("composer.json file is missing") + ErrMissingComposerFile = util.NewInvalidArgumentErrorf("composer.json file is missing") // ErrInvalidName indicates an invalid package name - ErrInvalidName = errors.New("package name is invalid") + ErrInvalidName = util.NewInvalidArgumentErrorf("package name is invalid") // ErrInvalidVersion indicates an invalid package version - ErrInvalidVersion = errors.New("package version is invalid") + 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 5bb3fb8930..de11dbee45 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 37a5170dd3..58f268bd48 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 <Name>/<Version>@<User>/<Channel>#<Revision> diff --git a/modules/packages/helm/metadata.go b/modules/packages/helm/metadata.go index 98b5919a73..fdbd9003b8 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" @@ -18,13 +18,13 @@ import ( var ( // ErrMissingChartFile indicates a missing Chart.yaml file - ErrMissingChartFile = errors.New("Chart.yaml file is missing") + ErrMissingChartFile = util.NewInvalidArgumentErrorf("Chart.yaml file is missing") // ErrInvalidName indicates an invalid package name - ErrInvalidName = errors.New("package name is invalid") + ErrInvalidName = util.NewInvalidArgumentErrorf("package name is invalid") // ErrInvalidVersion indicates an invalid package version - ErrInvalidVersion = errors.New("package version is invalid") + ErrInvalidVersion = util.NewInvalidArgumentErrorf("package version is invalid") // ErrInvalidChart indicates an invalid chart - ErrInvalidChart = errors.New("chart is invalid") + 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 02f6724812..548d7ed9e5 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,6 +15,7 @@ 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" @@ -23,15 +23,15 @@ import ( var ( // ErrInvalidPackage indicates an invalid package - ErrInvalidPackage = errors.New("The package is invalid") + ErrInvalidPackage = util.NewInvalidArgumentErrorf("package is invalid") // ErrInvalidPackageName indicates an invalid name - ErrInvalidPackageName = errors.New("The package name is invalid") + ErrInvalidPackageName = util.NewInvalidArgumentErrorf("package name is invalid") // ErrInvalidPackageVersion indicates an invalid version - ErrInvalidPackageVersion = errors.New("The package version is invalid") + ErrInvalidPackageVersion = util.NewInvalidArgumentErrorf("package version is invalid") // ErrInvalidAttachment indicates a invalid attachment - ErrInvalidAttachment = errors.New("The package attachment is invalid") + ErrInvalidAttachment = util.NewInvalidArgumentErrorf("package attachment is invalid") // ErrInvalidIntegrity indicates an integrity validation error - ErrInvalidIntegrity = errors.New("Failed to validate integrity") + 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 033421af96..3c478b1c02 100644 --- a/modules/packages/nuget/metadata.go +++ b/modules/packages/nuget/metadata.go @@ -7,13 +7,13 @@ 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" @@ -21,13 +21,13 @@ import ( var ( // ErrMissingNuspecFile indicates a missing Nuspec file - ErrMissingNuspecFile = errors.New("Nuspec file is missing") + ErrMissingNuspecFile = util.NewInvalidArgumentErrorf("Nuspec file is missing") // ErrNuspecFileTooLarge indicates a Nuspec file which is too large - ErrNuspecFileTooLarge = errors.New("Nuspec file is too large") + ErrNuspecFileTooLarge = util.NewInvalidArgumentErrorf("Nuspec file is too large") // ErrNuspecInvalidID indicates an invalid id in the Nuspec file - ErrNuspecInvalidID = errors.New("Nuspec file contains an invalid id") + ErrNuspecInvalidID = util.NewInvalidArgumentErrorf("Nuspec file contains an invalid id") // ErrNuspecInvalidVersion indicates an invalid version in the Nuspec file - ErrNuspecInvalidVersion = errors.New("Nuspec file contains an invalid version") + 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 634bbb17e6..b709eac4c1 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.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 36fe665707..13a066afac 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.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 14fb755606..efb2ba34a4 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 ( @@ -31,9 +32,9 @@ const ( var ( // ErrUnsupportedType indicates an unsupported type - ErrUnsupportedType = errors.New("Type is unsupported") + ErrUnsupportedType = util.NewInvalidArgumentErrorf("type is unsupported") // ErrInvalidIntRange indicates an invalid number range - ErrInvalidIntRange = errors.New("Number is not in valid 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 e2c73c8f3a..adc1c05808 100644 --- a/modules/packages/rubygems/metadata.go +++ b/modules/packages/rubygems/metadata.go @@ -6,11 +6,11 @@ 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" @@ -18,11 +18,11 @@ import ( var ( // ErrMissingMetadataFile indicates a missing metadata.gz file - ErrMissingMetadataFile = errors.New("Metadata file is missing") + ErrMissingMetadataFile = util.NewInvalidArgumentErrorf("metadata.gz file is missing") // ErrInvalidName indicates an invalid id in the metadata.gz file - ErrInvalidName = errors.New("Metadata file contains an invalid name") + ErrInvalidName = util.NewInvalidArgumentErrorf("package name is invalid") // ErrInvalidVersion indicates an invalid version in the metadata.gz file - ErrInvalidVersion = errors.New("Metadata file contains an invalid version") + 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 63bd447bf3..e67b9977f0 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...) +} |