how to set dynamic payloads in raise fault policy

Not applicable
I have a condition which looks for 2 mandatory quesyparams "A" and "B"(say). If any of them is null then I need to raise a fault and set a payload which should only mention the name of queryparam which is null (or both A and B if both are null). What I can do currently is hardcoding the payload with text as- "A and B cannot be null" But if only A is null I need to say- "A cannot b null" How do I dynamically do this, as I cannot use the <condition> there.
0 6 1,925
6 REPLIES 6

Dear @Pranjali Shrivastava ,

You cannot achieve same using single Raise Fault policy, but what you can do is create two raise fault policies and do the same. For Example,

    <PreFlow name="PreFlow">

        <Request>

            <Step>

                <Name>Raise-Fault-1</Name>

                <Condition>request.queryparam.a = null</Condition>

            </Step>

            <Step>

                <Name>Raise-Fault-2</Name>

                <Condition>request.queryparam.w = null</Condition>

            </Step>

        </Request>

        <Response/>

    </PreFlow>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RaiseFault async="false" continueOnError="false" enabled="true" name="Raise-Fault-1">
    <DisplayName>Raise Fault-1</DisplayName>
    <Properties/>
    <FaultResponse>
        <Set>
            <Headers/>
            <Payload contentType="text/plain">Queryparam "A" not found</Payload>
            <StatusCode>404</StatusCode>
            <ReasonPhrase>Queryparam "A" not found</ReasonPhrase>
        </Set>
    </FaultResponse>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RaiseFault async="false" continueOnError="false" enabled="true" name="Raise-Fault-2">
    <DisplayName>Raise Fault-2</DisplayName>
    <Properties/>
    <FaultResponse>
        <Set>
            <Headers/>
            <Payload contentType="text/plain">Queryparam "W" not found</Payload>
            <StatusCode>404</StatusCode>
            <ReasonPhrase>Query Param "W" Not found</ReasonPhrase>
        </Set>
    </FaultResponse>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault>

See attached proxy which explains same on yahooWeather API.

Or,

You can use Javascript Policy to do this using a single policy.

Cheers,

Anil Sagar

Thankyou 🙂

Dear @Pranjali Shrivastava ,

Is your issue resolved ? If Yes, Please click on Accept link below answer so that it will be helpful for others looking for similar solution.

Thanks,

Anil Sagar

Adding to @Anil Sagar response, It might be easier to set all these values in your Javascript and use them in the Raisefault policy For eg, in your JS,
//check for condition A failing

context.setVariable('fault_payload','Param A is missing')

context.setVariable('fault_code',400)

...

..
//check for condition B failing

context.setVariable('fault_payload','Param B is missing')

context.setVariable('fault_code',400)

...


Then you could have one generic Raisefault policy like this,
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RaiseFault async="false" continueOnError="false" enabled="true" name="Raise-Fault-2">
    <DisplayName>Raise Fault-2</DisplayName>
    <Properties/>
    <FaultResponse>
        <Set>
            <Headers/>
            <Payload contentType="text/plain">{fault_payload}</Payload>
            <StatusCode>{fault_code}</StatusCode>
            <ReasonPhrase>{fault_reason}</ReasonPhrase>
        </Set>
    </FaultResponse>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault>

in this case how do I figure out if both the parameters are failing. Then I need the names for both in the payload

that would just be another condition in the js, right?

//check for both condition A & B failing

context.setVariable('fault_payload','Param A & B is missing')

context.setVariable('fault_code',400)