forked from rai-project/dlframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
86 lines (71 loc) · 1.79 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
package http
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/go-openapi/runtime"
"github.com/pkg/errors"
rerrors "github.com/go-openapi/errors"
)
type Error struct {
message error
code int
name string
rw http.ResponseWriter
producer runtime.Producer
}
func NewError(name string, message error) *Error {
return &Error{
message: message,
code: http.StatusBadRequest,
name: name,
}
}
func (e *Error) MarshalJSON() ([]byte, error) {
name := fmt.Sprintf("\"name\": \"%v\"", e.name)
message := fmt.Sprintf("\"message\": \"%v\"", e.message)
code := fmt.Sprintf("\"code\": %v", e.code)
var stack string
stackData := strings.Split(fmt.Sprintf("%+v", errors.WithStack(e.message)), "\n")
bts, err := json.Marshal(stackData)
if err != nil {
stack = fmt.Sprintf("\"stack\": []")
} else {
stack = fmt.Sprintf("\"stack\": %s", string(bts))
}
res := fmt.Sprintf("{%s, %s, %s, %s}", name, message, code, stack)
return []byte(res), nil
}
func (e *Error) Error() string {
return fmt.Sprintf("%s:%v", e.name, e.message.Error())
}
func (e *Error) WithCode(code int) *Error {
e.code = code
return e
}
func (e *Error) WithName(name string) *Error {
e.name = name
return e
}
func (e *Error) WithMessage(message error) *Error {
e.message = message
return e
}
func (e *Error) Code() int32 {
return int32(e.code)
}
func (e *Error) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) {
rw.WriteHeader(e.code)
producer.Produce(rw, e)
}
// ServeError the error handler interface implemenation
func ServeError(rw http.ResponseWriter, r *http.Request, err error) {
rw.Header().Set("Content-Type", "application/json")
switch e := err.(type) {
case *Error:
e.WriteResponse(rw, runtime.JSONProducer())
default:
rerrors.ServeError(rw, r, err)
}
}