mirror of https://github.com/hashicorp/packer
parent
c09f8b84b9
commit
b7e32cb10a
@ -0,0 +1,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))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
ERROR: -> DeploymentFailed : At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details.
|
||||
ERROR: -> BadRequest
|
||||
ERROR: -> InvalidRequestFormat : Cannot parse the request.
|
||||
ERROR: -> InvalidJson : Error converting value "playground" to type 'Microsoft.WindowsAzure.Networking.Nrp.Frontend.Contract.Csm.Public.IpAllocationMethod'. Path 'properties.publicIPAllocationMethod', line 1, position 130.
|
||||
@ -0,0 +1 @@
|
||||
ERROR: -> ResourceNotFound : The Resource 'Microsoft.Compute/images/PackerUbuntuImage' under resource group 'packer-test00' was not found.
|
||||
@ -0,0 +1,76 @@
|
||||
package arm
|
||||
|
||||
import (
|
||||
"github.com/approvals/go-approval-tests"
|
||||
"github.com/hashicorp/packer/common/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const AzureErrorSimple = `{"error":{"code":"ResourceNotFound","message":"The Resource 'Microsoft.Compute/images/PackerUbuntuImage' under resource group 'packer-test00' was not found."}}`
|
||||
const AzureErrorNested = `{"status":"Failed","error":{"code":"DeploymentFailed","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details.","details":[{"code":"BadRequest","message":"{\r\n \"error\": {\r\n \"code\": \"InvalidRequestFormat\",\r\n \"message\": \"Cannot parse the request.\",\r\n \"details\": [\r\n {\r\n \"code\": \"InvalidJson\",\r\n \"message\": \"Error converting value \\\"playground\\\" to type 'Microsoft.WindowsAzure.Networking.Nrp.Frontend.Contract.Csm.Public.IpAllocationMethod'. Path 'properties.publicIPAllocationMethod', line 1, position 130.\"\r\n }\r\n ]\r\n }\r\n}"}]}}`
|
||||
|
||||
func TestAzureErrorSimpleShouldUnmarshal(t *testing.T) {
|
||||
var azureErrorReponse azureErrorResponse
|
||||
err := json.Unmarshal([]byte(AzureErrorSimple), &azureErrorReponse)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if azureErrorReponse.ErrorDetails.Code != "ResourceNotFound" {
|
||||
t.Errorf("Error.Code")
|
||||
}
|
||||
if azureErrorReponse.ErrorDetails.Message != "The Resource 'Microsoft.Compute/images/PackerUbuntuImage' under resource group 'packer-test00' was not found." {
|
||||
t.Errorf("Error.Message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAzureErrorNestedShouldUnmarshal(t *testing.T) {
|
||||
var azureError azureErrorResponse
|
||||
err := json.Unmarshal([]byte(AzureErrorNested), &azureError)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if azureError.ErrorDetails.Code != "DeploymentFailed" {
|
||||
t.Errorf("Error.Code")
|
||||
}
|
||||
if !strings.HasPrefix(azureError.ErrorDetails.Message, "At least one resource deployment operation failed") {
|
||||
t.Errorf("Error.Message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAzureErrorEmptyShouldFormat(t *testing.T) {
|
||||
var aer azureErrorResponse
|
||||
s := aer.Error()
|
||||
|
||||
if s != "" {
|
||||
t.Fatalf("Expected \"\", but got %s", aer.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAzureErrorSimpleShouldFormat(t *testing.T) {
|
||||
var azureErrorReponse azureErrorResponse
|
||||
err := json.Unmarshal([]byte(AzureErrorSimple), &azureErrorReponse)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = approvaltests.VerifyString(t, azureErrorReponse.Error())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAzureErrorNestedShouldFormat(t *testing.T) {
|
||||
var azureErrorReponse azureErrorResponse
|
||||
err := json.Unmarshal([]byte(AzureErrorNested), &azureErrorReponse)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = approvaltests.VerifyString(t, azureErrorReponse.Error())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue