Skip to content

Commit

Permalink
Make runtime errors package errors more generic
Browse files Browse the repository at this point in the history
Quite a lot of the errors in the package had close to the same meaning,
or could be used together with a context defining wrapping error. They
also assumed knowledge of component implementation details at times.

This commit de-duplicates the and removes the errors in these category,
and also properly documents them.

Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Jul 7, 2021
1 parent bac7da8 commit 0db78f1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 66 deletions.
18 changes: 18 additions & 0 deletions runtime/errors/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2021 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package errors contains generic controller and reconciler runtime errors to be used by GitOps Toolkit components.
package errors
90 changes: 24 additions & 66 deletions runtime/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ import (
"k8s.io/apimachinery/pkg/types"
)

// ReconciliationError is returned on a reconciliation failure for a resource,
// it includes the Kind and NamespacedName of the resource the reconciliation
// was performed for, and the underlying Err.
// ReconciliationError is describes a generic reconciliation error for a resource, it includes the Kind and NamespacedName
// of the resource, and any underlying Err.
type ReconciliationError struct {
Kind string
NamespacedName types.NamespacedName
Expand All @@ -39,93 +38,52 @@ func (e *ReconciliationError) Unwrap() error {
return e.Err
}

// SourceNotReadyError is returned when a source is not in a ready condition
// during a reconciliation attempt, it includes the Kind and NamespacedName of
// the source.
type SourceNotReadyError struct {
// ResourceNotReadyError describes an error in which a referred resource is not in a meta.ReadyCondition state,
// it includes the Kind and NamespacedName, and any underlying Err.
type ResourceNotReadyError struct {
Kind string
NamespacedName types.NamespacedName
Err error
}

func (e *ResourceNotReadyError) Error() string {
return fmt.Sprintf("%s resource '%s' is not ready", e.Kind, e.NamespacedName.String())
}

func (e *SourceNotReadyError) Error() string {
return fmt.Sprintf("%s source '%s' is not ready", e.Kind, e.NamespacedName.String())
func (e *ResourceNotReadyError) Unwrap() error {
return e.Err
}

// SourceNotFoundError is returned if a referred source was not found, it
// includes the Kind and NamespacedName of the source.
type SourceNotFoundError struct {
// ResourceNotFoundError describes an error in which a referred resource could not be found,
// it includes the Kind and NamespacedName, and any underlying Err.
type ResourceNotFoundError struct {
Kind string
NamespacedName types.NamespacedName
Err error
}

func (e *SourceNotFoundError) Error() string {
return fmt.Sprintf("%s source '%s' does not exist", e.Kind, e.NamespacedName.String())
func (e *ResourceNotFoundError) Error() string {
return fmt.Sprintf("%s resource '%s' could not be found", e.Kind, e.NamespacedName.String())
}

// UnsupportedSourceKindError is returned if a referred source is of an
// unsupported kind, it includes the Kind and Namespace of the source, and MAY
// contain a string slice of SupportedKinds.
type UnsupportedSourceKindError struct {
// UnsupportedResourceKindError describes an error in which a referred resource is of an unsupported kind,
// it includes the Kind and NamespacedName of the resource, and any underlying Err.
type UnsupportedResourceKindError struct {
Kind string
NamespacedName types.NamespacedName
SupportedKinds []string
}

func (e *UnsupportedSourceKindError) Error() string {
func (e *UnsupportedResourceKindError) Error() string {
err := fmt.Sprintf("source '%s' with kind %s is not supported", e.NamespacedName.String(), e.Kind)
if len(e.SupportedKinds) == 0 {
return err
}
return fmt.Sprintf("%s (must be one of: %q)", err, e.SupportedKinds)
}

// ArtifactAcquisitionError is returned if the artifact of a source could not be
// acquired, it includes the Kind and NamespacedName of the source that
// advertised the artifact, and MAY contain an underlying Err.
type ArtifactAcquisitionError struct {
Kind string
NamespacedName types.NamespacedName
Err error
}

func (e *ArtifactAcquisitionError) Error() string {
err := fmt.Sprintf("failed to acquire %s artifact from '%s'", e.Kind, e.NamespacedName.String())
if e.Err == nil {
return err
}
return fmt.Sprintf("%s: %v", err, e.Err)
}

func (e *ArtifactAcquisitionError) Unwrap() error {
return e.Err
}

// DependencyNotReadyError is returned if a referred dependency resource is not
// in a ready condition, it includes the Kind and NamespacedName of the
// dependency.
type DependencyNotReadyError struct {
Kind string
NamespacedName types.NamespacedName
}

func (e *DependencyNotReadyError) Error() string {
return fmt.Sprintf("dependency '%s' of kind %s is not ready", e.NamespacedName.String(), e.Kind)
}

// DependencyNotFoundError is returned if a referred dependency resource was not
// found, it includes the Kind and NamespacedName of the dependency.
type DependencyNotFoundError struct {
Kind string
NamespacedName types.NamespacedName
}

func (e *DependencyNotFoundError) Error() string {
return fmt.Sprintf("dependency '%s' of kind %s does not exist", e.NamespacedName, e.Kind)
}

// GarbageCollectionError is returned on a garbage collection failure for a
// resource, it includes the Kind and NamespacedName the garbage collection
// failed for, and the underlying Err.
// GarbageCollectionError is describes a garbage collection error for a resources, it includes the Kind and
// NamespacedName of the resource, and the underlying Err.
type GarbageCollectionError struct {
Kind string
NamespacedName types.NamespacedName
Expand Down

0 comments on commit 0db78f1

Please sign in to comment.