github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/arm/azure_error_response.go (about) 1 package arm 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 ) 8 9 type azureErrorDetails struct { 10 Code string `json:"code"` 11 Message string `json:"message"` 12 Details []azureErrorDetails `json:"details"` 13 } 14 15 type azureErrorResponse struct { 16 ErrorDetails azureErrorDetails `json:"error"` 17 } 18 19 func newAzureErrorResponse(s string) *azureErrorResponse { 20 var errorResponse azureErrorResponse 21 err := json.Unmarshal([]byte(s), &errorResponse) 22 if err == nil { 23 return &errorResponse 24 } 25 26 return nil 27 } 28 29 func (e *azureErrorDetails) isEmpty() bool { 30 return e.Code == "" 31 } 32 33 func (e *azureErrorResponse) isEmpty() bool { 34 return e.ErrorDetails.isEmpty() 35 } 36 37 func (e *azureErrorResponse) Error() string { 38 var buf bytes.Buffer 39 //buf.WriteString("-=-=- ERROR -=-=-") 40 formatAzureErrorResponse(e.ErrorDetails, &buf, "") 41 //buf.WriteString("-=-=- ERROR -=-=-") 42 return buf.String() 43 } 44 45 // format a Azure Error Response by recursing through the JSON structure. 46 // 47 // Errors may contain nested errors, which are JSON documents that have been 48 // serialized and escaped. Keep following this nesting all the way down... 49 func formatAzureErrorResponse(error azureErrorDetails, buf *bytes.Buffer, indent string) { 50 if error.isEmpty() { 51 return 52 } 53 54 buf.WriteString(fmt.Sprintf("ERROR: %s-> %s : %s\n", indent, error.Code, error.Message)) 55 for _, x := range error.Details { 56 newIndent := fmt.Sprintf("%s ", indent) 57 58 var aer azureErrorResponse 59 err := json.Unmarshal([]byte(x.Message), &aer) 60 if err == nil { 61 buf.WriteString(fmt.Sprintf("ERROR: %s-> %s\n", newIndent, x.Code)) 62 formatAzureErrorResponse(aer.ErrorDetails, buf, newIndent) 63 } else { 64 buf.WriteString(fmt.Sprintf("ERROR: %s-> %s : %s\n", newIndent, x.Code, x.Message)) 65 } 66 } 67 }