-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
93 lines (80 loc) · 2.97 KB
/
handler.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2017 The Kubernetes Dashboard 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
import (
"log"
"net/http"
"k8s.io/apimachinery/pkg/api/errors"
)
// NonCriticalErrors is an array of error statuses, that are non-critical. That means, that this error can be
// silenced and displayed to the user as a warning on the frontend side.
var NonCriticalErrors = []int32{http.StatusForbidden, http.StatusUnauthorized}
// HandleError handles single error, that occurred during API GET call. If it is not critical, then it will be
// returned as a part of error array. Otherwise, it will be returned as a second value. Usage of this functions
// allows to distinguish critical errors from non-critical ones. It is needed to handle them in a different way.
func HandleError(err error) ([]error, error) {
nonCriticalErrors := make([]error, 0)
return AppendError(err, nonCriticalErrors)
}
// AppendError handles single error, that occurred during API GET call. If it is not critical, then it will be
// returned as a part of error array. Otherwise, it will be returned as a second value. Usage of this functions
// allows to distinguish critical errors from non-critical ones. It is needed to handle them in a different way.
func AppendError(err error, nonCriticalErrors []error) ([]error, error) {
if err != nil {
if isErrorCritical(err) {
return nonCriticalErrors, LocalizeError(err)
} else {
log.Printf("Non-critical error occurred during resource retrieval: %s", err)
nonCriticalErrors = appendMissing(nonCriticalErrors, LocalizeError(err))
}
}
return nonCriticalErrors, nil
}
// MergeErrors merges multiple non-critical error arrays into one array.
func MergeErrors(errorArraysToMerge ...[]error) (mergedErrors []error) {
for _, errorArray := range errorArraysToMerge {
mergedErrors = appendMissing(mergedErrors, errorArray...)
}
return
}
func isErrorCritical(err error) bool {
status, ok := err.(*errors.StatusError)
if !ok {
// Assume, that error is critical if it cannot be mapped.
return true
}
return !contains(NonCriticalErrors, status.ErrStatus.Code)
}
func appendMissing(slice []error, toAppend ...error) []error {
m := make(map[string]bool, 0)
for _, s := range slice {
m[s.Error()] = true
}
for _, a := range toAppend {
_, ok := m[a.Error()]
if !ok {
slice = append(slice, a)
m[a.Error()] = true
}
}
return slice
}
func contains(s []int32, e int32) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}