-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patherrors.go
108 lines (98 loc) · 2.83 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
105
106
107
108
// Package stormrpc provides the functionality for creating RPC servers/clients that communicate via NATS.
package stormrpc
import (
"errors"
"fmt"
)
// ErrorCode represents an enum type for stormRPC error codes.
type ErrorCode int
// RPC ErrorCodes.
const (
ErrorCodeUnknown ErrorCode = 0
ErrorCodeInternal ErrorCode = 1
ErrorCodeNotFound ErrorCode = 2
ErrorCodeInvalidArgument ErrorCode = 3
ErrorCodeUnimplemented ErrorCode = 4
ErrorCodeUnauthenticated ErrorCode = 5
ErrorCodePermissionDenied ErrorCode = 6
ErrorCodeAlreadyExists ErrorCode = 7
ErrorCodeDeadlineExceeded ErrorCode = 8
)
func (c ErrorCode) String() string {
switch c {
case ErrorCodeInternal:
return "STORMRPC_CODE_INTERNAL"
case ErrorCodeNotFound:
return "STORMRPC_CODE_NOT_FOUND"
case ErrorCodeInvalidArgument:
return "STORMRPC_CODE_INVALID_ARGUMENT"
case ErrorCodeUnimplemented:
return "STORMRPC_CODE_UNIMPLEMENTED"
case ErrorCodeUnauthenticated:
return "STORMRPC_CODE_UNAUTHENTICATED"
case ErrorCodePermissionDenied:
return "STORMRPC_CODE_PERMISSION_DENIED"
case ErrorCodeAlreadyExists:
return "STORMRPC_CODE_ALREADY_EXISTS"
case ErrorCodeDeadlineExceeded:
return "STORMRPC_CODE_DEADLINE_EXCEEDED"
default:
return "STORMRPC_CODE_UNKNOWN"
}
}
// Error represents an RPC error.
type Error struct {
Message string
Code ErrorCode
}
// Error allows for the Error type to conform to the built-in error interface.
func (e Error) Error() string {
return fmt.Sprintf("%s: %s", e.Code.String(), e.Message)
}
// Errorf constructs a new RPC Error.
func Errorf(code ErrorCode, format string, args ...any) *Error {
return &Error{
Code: code,
Message: fmt.Sprintf(format, args...),
}
}
// CodeFromErr retrieves the ErrorCode from a given error.
// If the error is not of type Error, ErrorCodeUnknown is returned.
func CodeFromErr(err error) ErrorCode {
var e *Error
if errors.As(err, &e) {
return e.Code
}
return ErrorCodeUnknown
}
// MessageFromErr retrieves the message from a given error.
// If the error is not of type Error, "unknown error" is returned.
func MessageFromErr(err error) string {
var e *Error
if errors.As(err, &e) {
return e.Message
}
return "unknown error"
}
func codeFromString(s string) ErrorCode {
switch s {
case "STORMRPC_CODE_INTERNAL":
return ErrorCodeInternal
case "STORMRPC_CODE_NOT_FOUND":
return ErrorCodeNotFound
case "STORMRPC_CODE_INVALID_ARGUMENT":
return ErrorCodeInvalidArgument
case "STORMRPC_CODE_UNIMPLEMENTED":
return ErrorCodeUnimplemented
case "STORMRPC_CODE_UNAUTHENTICATED":
return ErrorCodeUnauthenticated
case "STORMRPC_CODE_PERMISSION_DENIED":
return ErrorCodePermissionDenied
case "STORMRPC_CODE_ALREADY_EXISTS":
return ErrorCodeAlreadyExists
case "STORMRPC_CODE_DEADLINE_EXCEEDED":
return ErrorCodeDeadlineExceeded
default:
return ErrorCodeUnknown
}
}