-
Notifications
You must be signed in to change notification settings - Fork 85
/
errors.go
71 lines (59 loc) · 2.85 KB
/
errors.go
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
68
69
70
71
package cloud
import (
"fmt"
"net/http"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/microsoftgraph/msgraph-sdk-go/models/odataerrors"
"github.com/pkg/errors"
)
const (
// GraphErrorCodeResourceNotFound is the error code for resource not found.
GraphErrorCodeResourceNotFound = "Request_ResourceNotFound"
// GraphErrorCodeMultipleObjectsWithSameKeyValue is the error code for multiple objects with same key value.
GraphErrorCodeMultipleObjectsWithSameKeyValue = "Request_MultipleObjectsWithSameKeyValue"
)
// GraphError is a custom error type for Graph API errors.
type GraphError struct {
Errorable odataerrors.MainErrorable
}
// IsNotFound returns true if the given error is a NotFound error.
func IsNotFound(err error) bool {
return strings.Contains(err.Error(), "not found")
}
// IsRoleAssignmentAlreadyDeleted returns true if the given error is a role assignment already deleted error.
// Ref: https://docs.microsoft.com/en-us/rest/api/authorization/role-assignments/delete#response
func IsRoleAssignmentAlreadyDeleted(err error) bool {
derr := &azcore.ResponseError{}
return errors.As(err, &derr) && derr.StatusCode == http.StatusNoContent
}
// IsRoleAssignmentExists returns true if the given error is a role assignment already exists error.
func IsRoleAssignmentExists(err error) bool {
derr := &azcore.ResponseError{}
return errors.As(err, &derr) && derr.StatusCode == http.StatusConflict
}
// IsFederatedCredentialNotFound returns true if the given error is a federated credential not found error.
func IsFederatedCredentialNotFound(err error) bool {
gerr := GraphError{}
return errors.As(err, &gerr) && *gerr.Errorable.GetCode() == GraphErrorCodeResourceNotFound
}
// IsFederatedCredentialAlreadyExists returns true if the given error is a federated credential already exists error.
// E1202 22:40:05.500821 867104 main.go:57] "failed to add federated identity credential" err="code: Request_MultipleObjectsWithSameKeyValue, message: FederatedIdentityCredential with name aramase-default-cred already exists."
func IsFederatedCredentialAlreadyExists(err error) bool {
gerr := GraphError{}
return errors.As(err, &gerr) && *gerr.Errorable.GetCode() == GraphErrorCodeMultipleObjectsWithSameKeyValue
}
// maybeExtractGraphError returns the additional information from the graph API error.
// ref: https://docs.microsoft.com/en-us/graph/errors#error-resource-type
// errors returned by the graph API aren't serialized today and this is a known issue: https://github.com/microsoftgraph/msgraph-sdk-go-core/issues/1
func maybeExtractGraphError(err error) error {
var oerr *odataerrors.ODataError
if errors.As(err, &oerr) {
return GraphError{Errorable: oerr.GetError()}
}
return err
}
// Error returns the error message.
func (e GraphError) Error() string {
return fmt.Sprintf("code: %s, message: %s", *e.Errorable.GetCode(), *e.Errorable.GetMessage())
}