-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcerror.go
103 lines (88 loc) · 2.69 KB
/
cerror.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
package cerror
import (
"net/http"
"reflect"
"strings"
)
// Error defines custom error
type Error interface {
ErrorType() ErrorType
ErrorCode() string
HttpStatusCode() int
Error() string
ErrorWithTrace() string
With(err error) Error
}
type customError struct {
Type ErrorType
message string
errorCode string
httpStatusCode int
traces string
buildError error
appendedErrors []error
}
// HttpStatusCode Expose the StatusCode
func (c customError) HttpStatusCode() int {
return c.httpStatusCode
}
// ErrorCode Expose the ErrorCode
func (c customError) ErrorCode() string {
return c.errorCode
}
// ErrorType Expose the ErrorType
func (c customError) ErrorType() ErrorType {
return c.Type
}
// ErrorWithTrace Expose the Error message
func (c customError) Error() string {
return c.message
}
// ErrorWithTrace Expose the Error message with trace
func (c customError) ErrorWithTrace() string {
var result strings.Builder
result.WriteString("[")
result.WriteString(c.errorCode)
result.WriteString("] ")
result.WriteString(c.message)
result.WriteString("\n")
for _, x := range c.appendedErrors {
result.WriteString(x.Error())
result.WriteString("\n")
}
result.WriteString(c.buildError.Error())
result.WriteString("\n")
result.WriteString(c.traces)
return result.String()
}
// With Append the low or high level Error
func (c customError) With(err error) Error {
if reflect.TypeOf(err).Kind() == reflect.TypeOf(customError{}).Kind() {
c.appendedErrors = append(c.appendedErrors, err.(error))
} else {
c.buildError = err
}
return c
}
// New Create an instance of Error with Type and Message
func New(errType ErrorType, message string) Error {
return NewWithErrorCodeAndHttpStatusCode(errType, message, getErrorCode(), http.StatusInternalServerError)
}
// NewWithErrorCode Create an instance of Error with errType, message, code
func NewWithErrorCode(errType ErrorType, message string, errCode string) Error {
return NewWithErrorCodeAndHttpStatusCode(errType, message, errCode, http.StatusInternalServerError)
}
// NewWithHttpStatusCode Create an instance of Error with errType, message, code
func NewWithHttpStatusCode(errType ErrorType, message string, httpStatusCode int) Error {
return NewWithErrorCodeAndHttpStatusCode(errType, message, getErrorCode(), httpStatusCode)
}
// NewWithErrorCodeAndHttpStatusCode Create an instance of Error with extra errorCode and httpStatusCode
func NewWithErrorCodeAndHttpStatusCode(errType ErrorType, message, errorCode string, httpStatusCode int) Error {
return &customError{
Type: errType,
message: message,
traces: getStackTraces(),
errorCode: errorCode,
httpStatusCode: httpStatusCode,
}
}