-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
164 lines (139 loc) · 3.05 KB
/
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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package httpkit
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"runtime"
"strings"
)
// Error http错误
type Error struct {
header http.Header
body io.Reader
code int
err error
caller *runtime.Frame
}
// NewError 创建http错误
func NewError(code int) *Error {
return &Error{
code: code,
}
}
// WrapError 把其它错误转换为http错误
func WrapError(err error) *Error {
return &Error{
code: http.StatusInternalServerError,
err: err,
caller: GetCaller(),
}
}
// WrapErrorf 以指定的格式把错误转换为http错误
func WrapErrorf(format string, err error) *Error {
return &Error{
code: http.StatusInternalServerError,
err: fmt.Errorf(format, err),
caller: GetCaller(),
}
}
func (e Error) Error() string {
return http.StatusText(e.StatusCode())
}
// Unwrap returns cause error
func (e Error) Unwrap() error {
return e.err
}
// StatusCode returns response status code
func (e Error) StatusCode() int {
if code := e.code; code > 0 {
return code
}
return http.StatusInternalServerError
}
// Header returns response header
func (e Error) Header() http.Header {
return e.header
}
// Body returns response body reader
func (e Error) Body() io.Reader {
return e.body
}
// Caller returns WrapError caller
func (e Error) Caller() (*runtime.Frame, bool) {
return e.caller, e.caller != nil
}
// WithStatus set response status code
func (e *Error) WithStatus(code int) *Error {
e.code = code
return e
}
// WithHeader set response header
func (e *Error) WithHeader(key, value string) *Error {
if e.header == nil {
e.header = http.Header{}
}
e.header.Set(key, value)
return e
}
// WithBody set response body
func (e *Error) WithBody(r io.Reader) *Error {
e.body = r
return e
}
// WithJSON set json content as response body
func (e *Error) WithJSON(v interface{}) error {
data, err := json.Marshal(v)
if err != nil {
return fmt.Errorf("encode error response, %w", err)
}
e.WithHeader("Content-Type", "application/json")
return e.WithBytes(data)
}
// WithString set response body content
func (e *Error) WithString(s string) *Error {
e.WithHeader("Content-Type", "text/plain")
e.body = strings.NewReader(s)
return e
}
// WithBytes set response body content
func (e *Error) WithBytes(data []byte) *Error {
e.body = bytes.NewReader(data)
return e
}
// Panic with error
func (e *Error) Panic() {
panic(e)
}
// WriteError write error response
func WriteError(w http.ResponseWriter, httpError *Error) error {
if h := httpError.Header(); h != nil {
for key, values := range h {
for _, value := range values {
w.Header().Set(key, value)
break
}
}
}
w.WriteHeader(httpError.StatusCode())
if r := httpError.Body(); r != nil {
if _, err := io.Copy(w, r); err != nil {
return err
}
}
return nil
}
// GetCaller 返回当前函数的调用者
func GetCaller() *runtime.Frame {
pc := make([]uintptr, 2)
n := runtime.Callers(3, pc)
if n == 0 {
panic(errors.New("unknown caller"))
}
pc = pc[:n]
frames := runtime.CallersFrames(pc)
frame, _ := frames.Next()
return &frame
}