-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
39 lines (35 loc) · 819 Bytes
/
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
package foxyproxy
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
)
// Error is an api error object.
type Error struct {
Timestamp string `json:"timestamp"`
Status int `json:"status"`
ErrorString string `json:"error"`
Message string `json:"message"`
Path string `json:"path"`
}
// Error returns a string representation of Error.
func (e *Error) Error() string {
errBytes, err := json.MarshalIndent(e, "", "\t")
if err != nil {
return err.Error()
}
return string(errBytes)
}
// NewError generates a new Error object.
func NewError(body io.ReadCloser) (*Error, error) {
bodyBytes, err := ioutil.ReadAll(body)
if err != nil {
return nil, err
}
e := &Error{}
if err := json.Unmarshal(bodyBytes, e); err != nil {
return nil, fmt.Errorf(string(bodyBytes))
}
return e, nil
}