-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy patherrors.go
116 lines (95 loc) · 3.03 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
109
110
111
112
113
114
115
116
package error
import (
"fmt"
"github.com/pkg/errors"
)
// ErrorType is the type of an error
type ErrorType uint
const (
// NoType error
NoType ErrorType = iota
// BadRequest error
BadRequest
// NotFound error
NotFound
)
type customError struct {
errorType ErrorType
originalError error
context errorContext
}
type errorContext struct {
Field string
Message string
}
// New create a new customError
func (errorType ErrorType) New(msg string) error {
return customError{errorType: errorType, originalError: errors.New(msg)}
}
// Newf creates a new customError with formatted message
func (errorType ErrorType) Newf(msg string, args ...interface{}) error {
return customError{errorType: errorType, originalError: fmt.Errorf(msg, args...)}
}
// Wrap creates a new wrapped error
func (errorType ErrorType) Wrap(err error, msg string) error {
return errorType.Wrapf(err, msg)
}
// Wrapf creates a new wrapped error with format message
func (errorType ErrorType) Wrapf(err error, msg string, args ...interface{}) error {
return customError{errorType: errorType, originalError: errors.Wrapf(err, msg, args...)}
}
// Error returns the message of a customError
func (error customError) Error() string {
return error.originalError.Error()
}
// New creates a no type error
func New(msg string) error {
return customError{errorType: NoType, originalError: errors.New(msg)}
}
// Newf creates a no type rerror with formatted message
func Newf(msg string, args ...interface{}) error {
return customError{errorType: NoType, originalError: errors.New(fmt.Sprintf(msg, args...))}
}
// Wrap an error with a string
func Wrap(err error, msg string) error {
return Wrapf(err, msg)
}
// Cause gives the original error
func Cause(err error) error {
return errors.Cause(err)
}
// Wrapf an error with format sring
func Wrapf(err error, msg string, args ...interface{}) error {
wrappedError := errors.Wrapf(err, msg, args...)
if customErr, ok := err.(customError); ok {
return customError{
errorType: customErr.errorType,
originalError: wrappedError,
context: customErr.context,
}
}
return customError{errorType: NoType, originalError: wrappedError}
}
// AddErrorContext adds a context to an error
func AddErrorContext(err error, field, message string) error {
context := errorContext{Field: field, Message: message}
if customErr, ok := err.(customError); ok {
return customError{errorType: customErr.errorType, originalError: customErr.originalError, context: context}
}
return customError{errorType: NoType, originalError: err, context: context}
}
// GetErrorContext returns the error context
func GetErrorContext(err error) map[string]string {
emptyContext := errorContext{}
if customErr, ok := err.(customError); ok || customErr.context != emptyContext {
return map[string]string{"field": customErr.context.Field, "message": customErr.context.Message}
}
return nil
}
// GetType returns the error type
func GetType(err error) ErrorType {
if customErr, ok := err.(customError); ok {
return customErr.errorType
}
return NoType
}