-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy patherrors.go
53 lines (44 loc) · 1.11 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
51
52
53
// Copyright (c) 2020, Randy Westlund. All rights reserved.
// This code is under the BSD-2-Clause license.
package quickbooks
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
)
// 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"`
}
// Error implements the error interface.
func (f Failure) Error() string {
text, err := json.Marshal(f)
if err != nil {
return fmt.Sprintf("unexpected error while marshalling error: %v", err)
}
return string(text)
}
// parseFailure takes a response reader and tries to parse a Failure.
func parseFailure(resp *http.Response) error {
msg, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.New("When reading response body:" + err.Error())
}
var errStruct Failure
if err = json.Unmarshal(msg, &errStruct); err != nil {
return errors.New(strconv.Itoa(resp.StatusCode) + " " + string(msg))
}
return errStruct
}