forked from xmidt-org/kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
104 lines (86 loc) · 2.29 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
package kratos
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/xmidt-org/wrp-go/v3"
)
const (
StatusDeviceDisconnected int = 523
StatusDeviceTimeout int = 524
)
type Message struct {
Code int `json:"code"`
Body string `json:"body"`
}
// statusCode follows the go-kit convention. Errors and other objects that implement
// this interface are allowed to supply an HTTP response status code.
type StatusCoder interface {
StatusCode() int
}
type MessageBodyer interface {
MessageBody() string
}
func (msg Message) String() string {
return fmt.Sprintf("%d:%s", msg.Code, msg.Body)
}
// httpError can provide a StatusCode or MessageBody from the response received.
type httpError struct {
Message Message
SubError error
}
func createHTTPError(resp *http.Response, err error) *httpError {
var msg Message
defer resp.Body.Close()
data, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(data, &msg)
if msg.Body == "" {
switch resp.StatusCode {
case StatusDeviceDisconnected:
msg.Body = "ErrorDeviceBusy"
case StatusDeviceTimeout:
msg.Body = "ErrorTransactionsClosed/ErrorTransactionsAlreadyClosed/ErrorDeviceClosed"
default:
msg.Body = http.StatusText(msg.Code)
}
}
return &httpError{
Message: msg,
SubError: err,
}
}
func (e *httpError) Error() string {
return fmt.Sprintf("message: %s with error: %s", e.Message, e.SubError.Error())
}
func (e *httpError) StatusCode() int {
return e.Message.Code
}
func (e *httpError) MessageBody() string {
return e.Message.Body
}
// errors provides a way to supply and parse a list of errors
type errorList []error
func (es errorList) Error() string {
errStrings := []string{}
for _, err := range es {
errStrings = append(errStrings, err.Error())
}
return fmt.Sprintf("multiple errors: [%v]", strings.Join(errStrings, ","))
}
func (es errorList) Errors() []error {
return es
}
func CreateErrorWRP(transaction string, dest string, src string, statusCode int64, err error) *wrp.Message {
response := wrp.Message{
Type: wrp.SimpleRequestResponseMessageType,
Destination: dest,
Source: src,
ContentType: "application/json",
Payload: []byte(fmt.Sprintf(`{"err":"%s"}`, err.Error())),
TransactionUUID: transaction,
}
response.SetStatus(statusCode)
return &response
}