From 9e0af86775843ec0edf97e900bd22aee9a12cbba Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Tue, 18 Jan 2022 21:22:43 +0100 Subject: [PATCH] ostree: rename errors and introduce ParameterComboError Shorter and clearer error types. - InvalidParameterError is now RefError. It is returned when a ref (the new ref or the parent ref) is invalid. - The ResolveRefError is not renamed. It is returned when a ref can't be resolved (e.g., ref not found at URL). New error type ParameterComboError. It is returned when a parent ref is specified but no URL, which is an invalid parameter combination. --- internal/cloudapi/v2/v2.go | 2 +- internal/ostree/errors.go | 22 +++++++++++++++++----- internal/ostree/ostree.go | 6 +++--- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/internal/cloudapi/v2/v2.go b/internal/cloudapi/v2/v2.go index 9b22b10da..66b91b9d5 100644 --- a/internal/cloudapi/v2/v2.go +++ b/internal/cloudapi/v2/v2.go @@ -286,7 +286,7 @@ func (h *apiHandlers) PostCompose(ctx echo.Context) error { } if imageOptions.OSTree, err = ostree.ResolveParams(ostreeOptions, imageType.OSTreeRef()); err != nil { switch v := err.(type) { - case ostree.InvalidParameterError: + case ostree.RefError: return HTTPError(ErrorInvalidOSTreeRef) case ostree.ResolveRefError: return HTTPErrorWithInternal(ErrorInvalidOSTreeRepo, v) diff --git a/internal/ostree/errors.go b/internal/ostree/errors.go index ac7aa8abd..2c32d3678 100644 --- a/internal/ostree/errors.go +++ b/internal/ostree/errors.go @@ -20,16 +20,28 @@ func NewResolveRefError(msg string, args ...interface{}) ResolveRefError { // InvalidParamsError is returned when a parameter is invalid (e.g., malformed // or contains illegal characters). -type InvalidParameterError struct { +type RefError struct { msg string } -func (e InvalidParameterError) Error() string { +func (e RefError) Error() string { return e.msg } -// NewInvalidParameterError creates and returns a new InvalidParameterError +// NewRefError creates and returns a new InvalidParameterError // with a given formatted message. -func NewInvalidParameterError(msg string, args ...interface{}) InvalidParameterError { - return InvalidParameterError{msg: fmt.Sprintf(msg, args...)} +func NewRefError(msg string, args ...interface{}) RefError { + return RefError{msg: fmt.Sprintf(msg, args...)} +} + +type ParameterComboError struct { + msg string +} + +func (e ParameterComboError) Error() string { + return e.msg +} + +func NewParameterComboError(msg string, args ...interface{}) ParameterComboError { + return ParameterComboError{msg: fmt.Sprintf(msg, args...)} } diff --git a/internal/ostree/ostree.go b/internal/ostree/ostree.go index 9d7e8dab9..3bee97715 100644 --- a/internal/ostree/ostree.go +++ b/internal/ostree/ostree.go @@ -62,17 +62,17 @@ func ResolveParams(params RequestParams, defaultRef string) (RequestParams, erro if resolved.Ref == "" { resolved.Ref = defaultRef } else if !VerifyRef(params.Ref) { // only verify if specified in params - return resolved, NewInvalidParameterError("Invalid ostree ref %q", params.Ref) + return resolved, NewRefError("Invalid ostree ref %q", params.Ref) } if params.Parent != "" { // parent must also be a valid ref if !VerifyRef(params.Parent) { - return resolved, NewInvalidParameterError("Invalid ostree parent ref %q", params.Parent) + return resolved, NewRefError("Invalid ostree parent ref %q", params.Parent) } if params.URL == "" { // specifying parent ref also requires URL - return resolved, NewInvalidParameterError("ostree parent ref specified, but no URL to retrieve it") + return resolved, NewParameterComboError("ostree parent ref specified, but no URL to retrieve it") } }