-
Notifications
You must be signed in to change notification settings - Fork 12
/
errors.go
67 lines (59 loc) · 1.75 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
//
// Copyright (c) 2020 SSH Communications Security Inc.
//
// All rights reserved.
//
package restapi
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
// ErrorResponse contains REST endpoint error response information.
type ErrorResponse struct {
ErrorCode string `json:"error_code"`
ErrorMessage string `json:"error_message,omitempty"`
Property string `json:"property,omitempty"`
Details []ErrorDetail `json:"details,omitempty"`
}
// ErrorDetail contains detailed error information, linked with the
// error response.
type ErrorDetail struct {
ErrorCode string `json:"error_code"`
ErrorMessage string `json:"error_message,omitempty"`
Property string `json:"property,omitempty"`
}
// ErrorFromResponse creates an error value from the REST API error
// response.
func ErrorFromResponse(r *http.Response, responseBody []byte) error {
if len(responseBody) == 0 {
return fmt.Errorf("HTTP error: %s", r.Status)
}
response := new(ErrorResponse)
err := json.Unmarshal(responseBody, response)
if err != nil {
return fmt.Errorf("HTTP error: %s (unexpected response body: %s)",
r.Status, err)
}
msg := fmt.Sprintf("error: %s", response.ErrorCode)
if len(response.ErrorMessage) > 0 {
msg += fmt.Sprintf(", message: %s", response.ErrorMessage)
}
if len(response.Property) > 0 {
msg += fmt.Sprintf(", property: %s", response.Property)
}
if len(response.Details) > 0 {
for _, detail := range response.Details {
msg += fmt.Sprintf(", {error: %s", detail.ErrorCode)
if len(detail.ErrorMessage) > 0 {
msg += fmt.Sprintf(", message: %s", detail.ErrorMessage)
}
if len(detail.Property) > 0 {
msg += fmt.Sprintf(", property: %s", detail.Property)
}
msg += "}"
}
}
return errors.New(msg)
}