error_types.go 1.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package controllers

import (
	"errors"
	"fmt"
)

// ErrorConditions
var (
	// Generic temporary error
	ErrTemporary   = errors.New("TemporaryError")
	ErrInvalidSpec = errors.New("InvalidSpec")
	// ErrFunctionDomain can be returned by a function that was passed invalid arguments, such as the default case of a switch.
	// Consider constraining the arg type.
	ErrFunctionDomain = errors.New("FunctionDomainError")
	ErrClientK8s      = errors.New("k8sAPIClientError")
17
	ErrFilesystemIO   = errors.New("FilesystemIOError")
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
)

type reconcileError interface {
	error
	Wrap(msg string) reconcileError
	Unwrap() error
	Temporary() bool
}

// applicationError wraps an error condition and gives it more context from where it occurred
type applicationError struct {
	innerException error
	errorCondition error
}

func newApplicationError(inner, condition error) reconcileError {
	return &applicationError{
		innerException: inner,
		errorCondition: condition,
	}
}

func (e *applicationError) Wrap(msg string) reconcileError {
	return newApplicationError(fmt.Errorf("%s: %w", msg, e.innerException), e.errorCondition)
}

func (e *applicationError) Unwrap() error { return e.errorCondition }
func (e *applicationError) Error() string {
	if e.innerException == nil {
		return e.errorCondition.Error()
	}
	return fmt.Errorf("%s: %v", e.errorCondition.Error(), e.innerException).Error()
}

// Temporary shows if the error condition is temporary or permanent
func (e *applicationError) Temporary() bool {
	// NOTE: List all permanent errors
	switch e.errorCondition {
	case ErrInvalidSpec:
		return false
	case ErrFunctionDomain:
		return false
	default:
		return true
	}
}