This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
errors.go
82 lines (69 loc) · 1.71 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
package errors
import "fmt"
// Code represents an error code
type Code string
const (
// ErrCanceled is thrown when a transaction is canceled
ErrCanceled Code = "Canceled"
// ErrUnauthorized is thrown when the API user is unauthorized to perform
// the request
ErrUnauthorized Code = "Unauthorized"
// ErrInvalidInput is thrown when the API user input is invalid
ErrInvalidInput Code = "InvalidInput"
// ErrWrongCreds is thrown when the API user provides wrong authentication
// credentials
ErrWrongCreds Code = "WrongCreds"
)
// Error represents a typed store error
type Error struct {
Code string
Message string
}
// FilterCode turns unknown error codes to empty strings
func FilterCode(code Code) string {
switch code {
case ErrCanceled:
return string(code)
case ErrUnauthorized:
return string(code)
case ErrInvalidInput:
return string(code)
case ErrWrongCreds:
return string(code)
}
return ""
}
// New creates a new store error
func New(code Code, message string) Error {
return Error{
Code: FilterCode(code),
Message: message,
}
}
// Newf creates a new store error
func Newf(code Code, format string, v ...interface{}) Error {
return Error{
Code: FilterCode(code),
Message: fmt.Sprintf(format, v...),
}
}
// Wrap wraps an error into a new store error
func Wrap(code Code, err error) Error {
return Error{
Code: FilterCode(code),
Message: err.Error(),
}
}
// Error implements the error interface
func (err Error) Error() string {
return err.Message
}
// ErrorCode returns the code if the given error is a store error and has a code
// assigned, otherwise returns an empty string
func ErrorCode(err error) string {
r, ok := err.(Error)
if !ok {
return ""
}
return r.Code
}