将项目添加到服务总线的Azure API管理策略-应该返回错误

问题描述

我有以下APIM政策:-

<policies>
    <inbound>
        <base />
        <set-variable name="payload" value="@(context.Request.Body?.As<JObject>(true))" />
            
        <!-- Put it onto the service bus -->
        <rewrite-uri template="/transaction/messages2" copy-unmatched-params="true" />

        <cache-lookup-value key="emlsbsas" variable-name="cachedSasToken" />
        <choose>
            <when condition="@(context.Variables.GetValueOrDefault&lt;string>("cachedSasToken") == null)">
                <cache-store-value key="emlsbsas" value="@{
                        string resourceUri = "{{Transaction_Uri}}";
                        string keyName = "{{Transaction_KeyName}}";
                        string key = "{{Transaction_Key}}";
                        TimeSpan sinceEpoch = DateTime.UtcNow - new DateTime(1970,1,1);
                        var expiry = Convert.ToString((int)sinceEpoch.TotalSeconds + 120);
                        string stringToSign = System.Uri.EscapeDataString(resourceUri) + "\n" + expiry;
                        HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
                        var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
                        var sasToken = String.Format("SharedAccessSignature sr={0}&amp;sig={1}&amp;se={2}&amp;skn={3}",System.Uri.EscapeDataString(resourceUri),System.Uri.EscapeDataString(signature),expiry,keyName);
                        return sasToken;
                    }" duration="10" />
                <cache-lookup-value key="emlsbsas" variable-name="cachedSasToken" />
            </when>
        </choose>
        <set-header name="Authorization" exists-action="override">
            <value>@(context.Variables.GetValueOrDefault<string>("cachedSasToken"))</value>
        </set-header>
        <set-header name="Content-type" exists-action="override">
            <value>application/json</value>
        </set-header>
        <set-header name="Ocp-Apim-Subscription-Key" exists-action="delete" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
        <return-response>
            <set-status code="200" reason="OK" />
            <set-header name="Content-Type" exists-action="override">
                <value>application/json</value>
            </set-header>
            <set-body>@{ 
                var json = new JObject( 
                    new JProperty("id",context.Request.Body?.As<JObject>(true)["id"])
                ); 
                    
                return json.ToString(); 
            }</set-body>
        </return-response>
    </outbound>
    <on-error>
        <set-header name="ErrorSource" exists-action="override">
            <value>@(context.LastError.Source)</value>
        </set-header>
        <set-header name="ErrorReason" exists-action="override">
            <value>@(context.LastError.Reason)</value>
        </set-header>
        <set-header name="ErrorMessage" exists-action="override">
            <value>@(context.LastError.Message)</value>
        </set-header>
        <set-header name="ErrorScope" exists-action="override">
            <value>@(context.LastError.Scope)</value>
        </set-header>
        <set-header name="ErrorSection" exists-action="override">
            <value>@(context.LastError.Section)</value>
        </set-header>
        <set-header name="ErrorPath" exists-action="override">
            <value>@(context.LastError.Path)</value>
        </set-header>
        <set-header name="ErrorPolicyId" exists-action="override">
            <value>@(context.LastError.PolicyId)</value>
        </set-header>
        <set-header name="ErrorStatusCode" exists-action="override">
            <value>@(context.Response.StatusCode.ToString())</value>
        </set-header>
        <base />
    </on-error>
</policies>

我向呼叫者返回200,其中包含已发送消息的ID。

发生错误时,我想返回的是错误而不是200

我试图用无效的URL进行测试。这是无效的

/ transaction / messages2

但仍返回200

当服务总线不可用时,如何获取它以返回错误?

解决方法

我们直接检查出站部分中的错误:

<outbound>
    <base />
    <choose>
        <when condition="@(context.Response.StatusCode >= 400 && context.Response.StatusCode < 503)">
            <set-status code="500" reason="Queue failure" />
        </when>
        <when condition="@(context.Response.StatusCode == 503)">
            <set-status code="503" reason="Service unavailable" />
            <set-header name="Retry-After" exists-action="override">
                <value>300</value>
            </set-header>
        </when>
        <otherwise />
    </choose>
    <xml-to-json kind="direct" apply="content-type-xml" consider-accept-header="false" />
</outbound>

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...