forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure_error_response.go
67 lines (56 loc) · 1.71 KB
/
azure_error_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
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
package arm
import (
"bytes"
"encoding/json"
"fmt"
)
type azureErrorDetails struct {
Code string `json:"code"`
Message string `json:"message"`
Details []azureErrorDetails `json:"details"`
}
type azureErrorResponse struct {
ErrorDetails azureErrorDetails `json:"error"`
}
func newAzureErrorResponse(s string) *azureErrorResponse {
var errorResponse azureErrorResponse
err := json.Unmarshal([]byte(s), &errorResponse)
if err == nil {
return &errorResponse
}
return nil
}
func (e *azureErrorDetails) isEmpty() bool {
return e.Code == ""
}
func (e *azureErrorResponse) isEmpty() bool {
return e.ErrorDetails.isEmpty()
}
func (e *azureErrorResponse) Error() string {
var buf bytes.Buffer
//buf.WriteString("-=-=- ERROR -=-=-")
formatAzureErrorResponse(e.ErrorDetails, &buf, "")
//buf.WriteString("-=-=- ERROR -=-=-")
return buf.String()
}
// format a Azure Error Response by recursing through the JSON structure.
//
// Errors may contain nested errors, which are JSON documents that have been
// serialized and escaped. Keep following this nesting all the way down...
func formatAzureErrorResponse(error azureErrorDetails, buf *bytes.Buffer, indent string) {
if error.isEmpty() {
return
}
buf.WriteString(fmt.Sprintf("ERROR: %s-> %s : %s\n", indent, error.Code, error.Message))
for _, x := range error.Details {
newIndent := fmt.Sprintf("%s ", indent)
var aer azureErrorResponse
err := json.Unmarshal([]byte(x.Message), &aer)
if err == nil {
buf.WriteString(fmt.Sprintf("ERROR: %s-> %s\n", newIndent, x.Code))
formatAzureErrorResponse(aer.ErrorDetails, buf, newIndent)
} else {
buf.WriteString(fmt.Sprintf("ERROR: %s-> %s : %s\n", newIndent, x.Code, x.Message))
}
}
}