forked from open-cluster-management-io/sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
54 lines (45 loc) · 1.66 KB
/
Copy patherrors.go
File metadata and controls
54 lines (45 loc) · 1.66 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
package errors
import (
"encoding/json"
"fmt"
"net/http"
grpcstatus "google.golang.org/grpc/status"
apierros "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)
const StatusReasonPublishError metav1.StatusReason = "PublishError"
// ToStatusError converts the err to a kube status error
func ToStatusError(qualifiedResource schema.GroupResource, name string, err error) *apierros.StatusError {
grpcErr, ok := grpcstatus.FromError(err)
if !ok {
return NewPublishError(qualifiedResource, name, err)
}
var statusErr apierros.StatusError
if unmarshalErr := json.Unmarshal([]byte(grpcErr.Message()), &statusErr); unmarshalErr != nil {
return NewPublishError(qualifiedResource, name, err)
}
return &statusErr
}
// NewPublishError returns an error indicating a resource could not be published, and the client can try again.
func NewPublishError(qualifiedResource schema.GroupResource, name string, err error) *apierros.StatusError {
return &apierros.StatusError{
ErrStatus: metav1.Status{
Status: metav1.StatusFailure,
Code: http.StatusInternalServerError,
Reason: StatusReasonPublishError,
Details: &metav1.StatusDetails{
Group: qualifiedResource.Group,
Kind: qualifiedResource.Resource,
Name: name,
Causes: []metav1.StatusCause{{Message: err.Error()}},
},
Message: fmt.Sprintf("Failed to publish resource %s: %v", name, err),
},
}
}
// IsPublishError determines if err is a publish error which indicates that the request can be retried
// by the client.
func IsPublishError(err error) bool {
return apierros.ReasonForError(err) == StatusReasonPublishError
}