-
Notifications
You must be signed in to change notification settings - Fork 191
/
response.go
38 lines (31 loc) · 1.01 KB
/
response.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
package gopherduty
import "encoding/json"
// The response from calling the PagerDuty API. This can contain errors if the API call failed. Also, any errors
// encountered when calling the API is added to the Errors list.
type PagerDutyResponse struct {
Status string `json:"status"`
Message string `json:"message"`
IncidentKey string `json:"incident_key,omitempty"`
Errors []string `json:"errors,omitempty"`
}
// Return the JSON string.
func (p *PagerDutyResponse) String() string {
resp, _ := json.Marshal(p)
return string(resp)
}
// Error interface implementation.
func (p *PagerDutyResponse) Error() string {
return p.String()
}
// Returns true if there are any errors during API call.
func (p *PagerDutyResponse) HasErrors() bool {
return len(p.Errors) > 0
}
func (p *PagerDutyResponse) parse(rawResponse []byte) {
if err := json.Unmarshal(rawResponse, p); err != nil {
p.appendError(err)
}
}
func (p *PagerDutyResponse) appendError(err error) {
p.Errors = append(p.Errors, err.Error())
}