-
Notifications
You must be signed in to change notification settings - Fork 110
/
kferrors.go
70 lines (60 loc) · 1.96 KB
/
kferrors.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
// Copyright 2018 The Kubeflow 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 apis
import (
"fmt"
log "github.com/sirupsen/logrus"
)
type StatusCode int
const (
OK StatusCode = 200
INVALID_ARGUMENT StatusCode = 400
NOT_FOUND StatusCode = 404
INTERNAL_ERROR StatusCode = 500
UNKNOWN StatusCode = 520
)
// KfError stands for Kubeflow error. This is the standard error interface
// for Kubeflow components.
type KfError struct {
// Code is the HTTP response status code.
Code int `json:"code"`
Message string `json:"message,omitempty"`
}
func (e *KfError) Error() string {
return fmt.Sprintf(" (kubeflow.error): Code %d with message: %v",
e.Code, e.Message)
}
func IsNotFound(e error) bool {
kfError, ok := e.(*KfError)
return ok && kfError.Code == int(NOT_FOUND)
}
// NewKfErrorWithMessage will propogate the error with the given message.
//
// TODO(jlewi): Not sure this is the best way to propogate the error messages and turn them
// into KfErrors. There was a lot of code that was doing this but not asserting that the error
// was a KfError which was causing segmentation faults so I wrote this helper method.
func NewKfErrorWithMessage(e error, msg string) error {
kErr, ok := e.(*KfError)
if !ok {
log.Infof("Error is not a KfError; %v", e)
return &KfError{
Code: int(UNKNOWN),
Message: msg + "; " + e.Error(),
}
}
return &KfError{
Code: kErr.Code,
Message: msg + "; " + kErr.Message,
}
}