简体   繁体   English

Azure 自定义资源提供程序 - ARM 模板的自定义错误消息

[英]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?如果我的自定义资源提供程序想要向 ARM 返回自定义失败消息,我的响应正文应该是什么? I have a custom resource provider backed by a JavaScript Azure function I tried the following我有一个由 JavaScript Azure 函数支持的自定义资源提供程序,我尝试了以下操作

 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 - ARM 模板部署失败并出现错误 -

{
    "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 ARM 模板部署失败并出现错误

"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'."我希望 ARM 模板部署失败并显示我从 Azure 函数返回的自定义错误消息 - “自定义错误消息”。

Edited:编辑:

Here is my ARM template这是我的 ARM 模板

{
  "$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.但是,看起来有一个错误正在阻止错误通过 ARM 模板传播。 This should be fixed soon!这应该很快修复!

@jjbfour is right. @jjbfour 是对的。 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.我之前犯的错误是没有正确设置 HTTP 状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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