-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.go
123 lines (104 loc) · 2.86 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package solaredge
import (
"bytes"
"encoding/json"
"errors"
"golang.org/x/net/html"
)
// HTTPError contains the HTTP error received from the SolarEdge API server
type HTTPError struct {
StatusCode int
Status string
Body []byte
}
var _ error = &HTTPError{}
// Error returns a string representation of an HTTPError
func (e *HTTPError) Error() string { return e.Status }
// Is return true if e2 is an HTTPError
func (e *HTTPError) Is(e2 error) bool {
var HTTPError *HTTPError
ok := errors.As(e2, &HTTPError)
return ok
}
// ParseError wraps the error when failing to process the server response. Contains the original server response
// that generated the error, as well as the json error that triggered the error.
type ParseError struct {
Err error
Body []byte
}
var _ error = &ParseError{}
// Error returns a string representation of a ParseError
func (e *ParseError) Error() string {
return "json parse error: " + e.Err.Error()
}
// Unwrap returns the wrapped error
func (e *ParseError) Unwrap() error {
return e.Err
}
// Is returns true if e2 is a ParseError
func (e *ParseError) Is(e2 error) bool {
var parseError *ParseError
ok := errors.As(e2, &parseError)
return ok
}
// An APIError indicates the SolarEdge API server rejected the request (returning 403 - Forbidden).
//
// Apart from providing an invalid API key, this happens when the request arguments are not permitted,
// e.g. calling GetSiteEnergy() with a timeUnit of "DAY" and a time range of more than a year.
//
// WARNING: the SolarEdge API returns the error as an HTML document. APIError does its best to parse the document, but this may break at some point.
type APIError struct {
Message string
}
var _ error = &APIError{}
// Error returns a string representation of an APIError
func (e *APIError) Error() string {
return "api error: " + e.Message
}
// Is returns true if e2 is a ParseError
func (e *APIError) Is(e2 error) bool {
var APIError *APIError
return errors.As(e2, &APIError)
}
func makeAPIError(body []byte) *APIError {
if err := makeAPIErrorFromJSON(body); err != nil {
return err
}
return makeAPIErrorFromHTML(body)
}
func makeAPIErrorFromJSON(body []byte) *APIError {
var response struct {
String string `json:"String"`
}
if err := json.Unmarshal(body, &response); err == nil {
return &APIError{Message: response.String}
}
return nil
}
func makeAPIErrorFromHTML(body []byte) *APIError {
var atMessage bool
var message string
token := html.NewTokenizer(bytes.NewBuffer(body))
loop:
for {
switch token.Next() {
case html.ErrorToken:
break loop
case html.TextToken:
currentToken := token.Token()
if atMessage {
message = currentToken.String()
break loop
}
if currentToken.String() == "Message" {
atMessage = true
}
default:
//
}
}
if message == "" {
message = "could not return error reason"
}
return &APIError{Message: message}
}