-
Notifications
You must be signed in to change notification settings - Fork 7
/
errors.go
104 lines (97 loc) · 2.95 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2019-present Open Networking Foundation.
//
// 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 rsm
import (
"github.com/atomix/atomix-go-framework/pkg/atomix/errors"
)
// GetErrorFromStatus creates a typed error from a response status
func GetErrorFromStatus(status ResponseStatus) error {
switch status.Code {
case ResponseCode_OK:
return nil
case ResponseCode_ERROR:
return errors.NewUnknown(status.Message)
case ResponseCode_UNKNOWN:
return errors.NewUnknown(status.Message)
case ResponseCode_CANCELED:
return errors.NewCanceled(status.Message)
case ResponseCode_NOT_FOUND:
return errors.NewNotFound(status.Message)
case ResponseCode_ALREADY_EXISTS:
return errors.NewAlreadyExists(status.Message)
case ResponseCode_UNAUTHORIZED:
return errors.NewUnauthorized(status.Message)
case ResponseCode_FORBIDDEN:
return errors.NewForbidden(status.Message)
case ResponseCode_CONFLICT:
return errors.NewConflict(status.Message)
case ResponseCode_INVALID:
return errors.NewInvalid(status.Message)
case ResponseCode_UNAVAILABLE:
return errors.NewUnavailable(status.Message)
case ResponseCode_NOT_SUPPORTED:
return errors.NewNotSupported(status.Message)
case ResponseCode_TIMEOUT:
return errors.NewTimeout(status.Message)
case ResponseCode_INTERNAL:
return errors.NewInternal(status.Message)
default:
return errors.NewUnknown(status.Message)
}
}
// getStatus gets the proto status for the given error
func getCode(err error) ResponseCode {
if err == nil {
return ResponseCode_OK
}
typed, ok := err.(*errors.TypedError)
if !ok {
return ResponseCode_ERROR
}
switch typed.Type {
case errors.Unknown:
return ResponseCode_UNKNOWN
case errors.Canceled:
return ResponseCode_CANCELED
case errors.NotFound:
return ResponseCode_NOT_FOUND
case errors.AlreadyExists:
return ResponseCode_ALREADY_EXISTS
case errors.Unauthorized:
return ResponseCode_UNAUTHORIZED
case errors.Forbidden:
return ResponseCode_FORBIDDEN
case errors.Conflict:
return ResponseCode_CONFLICT
case errors.Invalid:
return ResponseCode_INVALID
case errors.Unavailable:
return ResponseCode_UNAVAILABLE
case errors.NotSupported:
return ResponseCode_NOT_SUPPORTED
case errors.Timeout:
return ResponseCode_TIMEOUT
case errors.Internal:
return ResponseCode_INTERNAL
default:
return ResponseCode_ERROR
}
}
// getMessage gets the message for the given error
func getMessage(err error) string {
if err == nil {
return ""
}
return err.Error()
}