forked from alexurquhart/qapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
43 lines (37 loc) · 837 Bytes
/
errors.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
package qapi
import (
"encoding/json"
"fmt"
"net/http"
)
type QuestradeError struct {
Code int `json:"code",string`
StatusCode int
Message string `json:"message"`
Endpoint string
OrderId int `json:"orderId,omitempty"`
Orders []Order `json:"orders,omitempty"`
}
func newQuestradeError(res *http.Response, body []byte) QuestradeError {
// Unmarshall the error text
var e QuestradeError
err := json.Unmarshal(body, &e)
if err != nil {
e.Code = -999
e.Message = string(body)
}
e.StatusCode = res.StatusCode
e.Endpoint = res.Request.URL.String()
return e
}
func (q QuestradeError) Error() string {
return fmt.Sprintf("\nQuestradeError:\n"+
"\tStatus code: HTTP %d\n"+
"\tEndpoint: %s\n"+
"\tError code: %d\n"+
"\tMessage: %s\n",
q.StatusCode,
q.Endpoint,
q.Code,
q.Message)
}