forked from rwestlund/quickbooks-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
50 lines (45 loc) · 1.08 KB
/
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
// Copyright (c) 2020, Randy Westlund. All rights reserved.
// This code is under the BSD-2-Clause license.
package quickbooks
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strconv"
)
// Error implements the error interface.
func (f Failure) Error() string {
var text, err = json.Marshal(f)
if err != nil {
return "When marshalling error:" + err.Error()
}
return string(text)
}
// Failure is the outermost struct that holds an error response.
type Failure struct {
Fault struct {
Error []struct {
Message string
Detail string
Code string `json:"code"`
Element string `json:"element"`
}
Type string `json:"type"`
}
Time Date `json:"time"`
}
// parseFailure takes a response reader and tries to parse a Failure.
func parseFailure(res *http.Response) error {
var msg, err = ioutil.ReadAll(res.Body)
if err != nil {
return errors.New("When reading response body:" + err.Error())
}
var errStruct Failure
err = json.Unmarshal(msg, &errStruct)
if err != nil {
return errors.New(strconv.Itoa(res.StatusCode) +
" " + string(msg))
}
return errStruct
}