-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
46 lines (38 loc) · 861 Bytes
/
error.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
package xecho
import (
"fmt"
"net/http"
"time"
)
type Error struct {
Code string `json:"code,omitempty"`
Detail string `json:"detail,omitempty"`
TrackID string `json:"track_id,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
responseCode int
}
func NewError(c XContext) *Error {
return &Error{
TrackID: c.ID(),
Timestamp: time.Now().Unix(),
responseCode: http.StatusInternalServerError,
}
}
func (e *Error) String() string {
return fmt.Sprintf("[%s] -> %s: %s", e.TrackID, e.Code, e.Detail)
}
func (e *Error) Error() string {
return e.String()
}
func (e *Error) WithCode(code string) *Error {
e.Code = code
return e
}
func (e *Error) WithDetail(detail error) *Error {
e.Detail = detail.Error()
return e
}
func (e *Error) WithResponseCode(code int) *Error {
e.responseCode = code
return e
}