简体   繁体   中英

Azure Custom resource provider - custom error message to ARM template

If my custom resource provider wants to return a custom failure message to ARM, what should be my response body? I have a custom resource provider backed by a JavaScript Azure function I tried the following

 body = {
       error: {
            code: "Failed",
            message: "A custom error message'."
        }
    };
httpStatus = 200;

 context.res = {
            status: httpStatus,
            headers: {
                'Content-Type': 'application/json'
              },
            body: body
        };

The ARM template deployment fails with error -

{
    "error": {
        "code": "ResourceDeploymentFailure",
        "message": "The response for resource had empty or invalid content."
    }

I also tried

 body = {
       properties: {
       provisioningState: "Failed",
       error: {
            code: "Failed",
            message: "A custom error message'."
        }
       }
    };
httpStatus = 200;

 context.res = {
            status: httpStatus,
            headers: {
                'Content-Type': 'application/json'
              },
            body: body
        };

The ARM template deployment fails with error

"The resource operation completed with terminal provisioning state 'Failed"

I want the ARM template deployment to fail with a custom error message I return form the Azure function - "A custom error message'."

Edited:

Here is my ARM template

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "resourcePrefix": {
      "type": "string",
      "defaultValue": "prfx-",
      "maxLength": 6,
      "metadata": {
        "description": "The prefix of HLF resource."
      }
    },
    "randomGuid": {
      "defaultValue": "[newGuid()]",
      "type": "string",
      "metadata": {
        "description": "New random GUID"
      }
    }
  },
  "variables": {
    "funcName": "[concat(parameters('resourcePrefix'), substring(parameters('randomGuid'), 0, 5))]",
    "myResourceProvider": "my-custom-provider",
    "location": "[resourceGroup().location]"
  },
  "resources": [
    {
      "apiVersion": "2018-09-01-preview",
      "type": "Microsoft.CustomProviders/resourceProviders",
      "name": "[variables('myResourceProvider')]",
      "location": "[variables('location')]",
      "properties": {
        "resourceTypes": [
          {
            "name": "deploy",
            "routingType": "Proxy",
            "endpoint": "<azure-func-url>"
          }
        ]
      }
    },
    {
      "apiVersion": "2018-09-01-preview",
      "type": "Microsoft.CustomProviders/resourceProviders/deploy",
      "name": "[concat(variables('myResourceProvider'), '/', variables('funcName'))]",
      "location": "[variables('location')]",
      "dependsOn": [
        "[concat('Microsoft.CustomProviders/resourceProviders/',variables('myResourceProvider'))]"
        ]      
    }
  ],
  "outputs": {
  }
}

Proxying the error message as is, is not currently supported for Custom Providers. The custom error message would be nested as details under a standard message.

However, it looks like there is a bug that is stopping the propagation of the error through the ARM template. This should be fixed soon!

@jjbfour is right. The custom message is nested under "Downstream" label in the propagated message. But that is fine for me. The following works

 body = {
       error: {
            code: "Failed",
            message: "A custom error message'."
        }
    };
httpStatus = 400;

 context.res = {
            status: httpStatus,
            headers: {
                'Content-Type': 'application/json'
              },
            body: body
        };

The mistake I was making earlier was not setting the HTTP status correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM