-
Notifications
You must be signed in to change notification settings - Fork 29
/
errors.go
50 lines (43 loc) · 1022 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
44
45
46
47
48
49
50
package goanda
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func newAPIError(request *http.Request, response *http.Response) APIError {
defer response.Body.Close()
msg := struct {
ErrorMessage string
RejectReason string
}{}
apiErr := APIError{
Response: response,
Request: request,
}
b, _ := ioutil.ReadAll(response.Body)
err := json.Unmarshal(b, &msg)
if err != nil {
apiErr.Message = string(b)
} else {
apiErr.Message = msg.RejectReason + msg.ErrorMessage
}
return apiErr
}
// APIError is returned when the Oanda server responds with an error
//
// Message is the returned error message from the server if possible to unmarshal,
// otherwise it is simply the entire body of the response
type APIError struct {
Request *http.Request
Response *http.Response
Message string
}
// APIError implements error
func (a APIError) Error() string {
return fmt.Sprintf("Oanda API Error [Url: %v, Response: %v]: %v",
a.Request.URL.String(),
a.Response.Status,
a.Message,
)
}