Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
67 lines (61 loc) · 2.51 KB
/
Copy patherrors.go
File metadata and controls
67 lines (61 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
64
65
66
67
package stackit
import (
"bytes"
"errors"
"net/http"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
)
// IsNotFound reports whether err is a STACKIT API "not found" (HTTP 404)
// error, e.g. because a resource has already been deleted out of band.
func IsNotFound(err error) bool {
var oapiErr *oapierror.GenericOpenAPIError
if errors.As(err, &oapiErr) {
return oapiErr.StatusCode == http.StatusNotFound
}
return false
}
// IsConflict reports whether err is a STACKIT API "conflict" (HTTP 409)
// error, e.g. because a resource has a dependent that has not finished
// deleting yet. Callers on the delete path should treat this as an expected,
// transient condition and requeue rather than surface it as a hard error.
func IsConflict(err error) bool {
var oapiErr *oapierror.GenericOpenAPIError
if errors.As(err, &oapiErr) {
return oapiErr.StatusCode == http.StatusConflict
}
return false
}
// IsAlreadyExists reports whether err is a STACKIT API "already exists"
// error (HTTP 409 with code "AlreadyExists"), e.g. because a create call
// raced with an earlier reconcile that already created the resource but
// whose status update has not yet reached the local cache.
func IsAlreadyExists(err error) bool {
var oapiErr *oapierror.GenericOpenAPIError
if errors.As(err, &oapiErr) {
return oapiErr.StatusCode == http.StatusConflict &&
bytes.Contains(oapiErr.Body, []byte("AlreadyExists"))
}
return false
}
// IsTransientAuthError reports whether err is STACKIT's token endpoint
// rejecting the SDK's authentication assertion specifically with "invalid
// iat". In practice this happens when the local clock is briefly skewed
// relative to STACKIT's (e.g. a WSL2 guest clock catching up right after the
// host wakes), and it reliably clears on the very next token request once
// the clock has settled. Callers should log this at a lower severity and
// requeue rather than surface it as a hard reconcile error.
//
// This deliberately checks for "invalid iat" rather than the broader
// "invalid_grant" error code: invalid_grant also covers permanent failures
// such as a revoked or expired service account key, which must not be
// silently retried forever - those should surface as a normal reconcile
// error instead.
func IsTransientAuthError(err error) bool {
var oapiErr *oapierror.GenericOpenAPIError
if errors.As(err, &oapiErr) {
return oapiErr.StatusCode == http.StatusBadRequest &&
bytes.Contains(oapiErr.Body, []byte("invalid_grant")) &&
bytes.Contains(oapiErr.Body, []byte("invalid iat"))
}
return false
}