How to use JavaScript variable in Raise fault ?

Not applicable

.

Say I created a script having below code:

var var1="hi";

used script caller in preflow and want to raise a fault and set payload to like below:

<set><payload>{hi}</payload></set>

it did not work.How to use custom javascript variables to Raise Fault? Please advice.

Solved Solved
1 10 5,686
1 ACCEPTED SOLUTION

@praveenkumar.kithuvaramesh ,

You cannot reference variables defined in your javascript module, from other policies.

It can seem confusing at first, but the way to think about it is this: the scope of variables you use in JavaScript is limited to the JavaScript callout. There is an entirely different scope of variables available through the flow of a message - this is called the "Message Context" in Apigee documentation. Policies like RaiseFault, AssignMessage or Quota can read or write variables from or into the message context.

To set a value into the message context from a JS module, you do this:

var var1="hi";
context.setVariable('hiVariable', var1);
// same as
context.setVariable('hiVariable', 'hi');

...and then you can reference that variable, 'hiVariable' in a policy like RaiseFault.

Your RaiseFault policy might be like this:

<RaiseFault name="Raise-Fault-1">
    <DisplayName>Raise Fault-1</DisplayName>
    <Properties/>
    <FaultResponse>
        <Set>
            <Headers/>
            <Payload contentType="text/plain">{hiVariable}</Payload>
            <StatusCode>500</StatusCode>
            <ReasonPhrase>Server Error</ReasonPhrase>
        </Set>
    </FaultResponse>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault> 

More about Flow variables here. Find more about how Javascript Object model works in Apigee here.

I hope it helps.

View solution in original post

10 REPLIES 10

@praveenkumar.kithuvaramesh ,

You cannot reference variables defined in your javascript module, from other policies.

It can seem confusing at first, but the way to think about it is this: the scope of variables you use in JavaScript is limited to the JavaScript callout. There is an entirely different scope of variables available through the flow of a message - this is called the "Message Context" in Apigee documentation. Policies like RaiseFault, AssignMessage or Quota can read or write variables from or into the message context.

To set a value into the message context from a JS module, you do this:

var var1="hi";
context.setVariable('hiVariable', var1);
// same as
context.setVariable('hiVariable', 'hi');

...and then you can reference that variable, 'hiVariable' in a policy like RaiseFault.

Your RaiseFault policy might be like this:

<RaiseFault name="Raise-Fault-1">
    <DisplayName>Raise Fault-1</DisplayName>
    <Properties/>
    <FaultResponse>
        <Set>
            <Headers/>
            <Payload contentType="text/plain">{hiVariable}</Payload>
            <StatusCode>500</StatusCode>
            <ReasonPhrase>Server Error</ReasonPhrase>
        </Set>
    </FaultResponse>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault> 

More about Flow variables here. Find more about how Javascript Object model works in Apigee here.

I hope it helps.

Thanks a lot Anil.

Hello Anil,

Can you please tell how to load contents of resource into Java script variable?

What do you mean "contents of resource" ?

Say I added a script of File Type XSD. Now In Javascript how do I access contentx of XSD script

@praveenkumar.kithuvaramesh , It's not supported out of the box, What exactly are you planning to do with contents of XSD script ?

I need to have a wsdl script file and then later change the target endpoingt of wsdl and reply back using Raise Fault.

I need to have a wsdl script file

Can you explain in further detail why you need a wsdl script file? What will the apiproxy DO with the wsdl script file?

Hello Dino and Anil,

When client does ?wsdl I need to retrieve the wsdl contents from wsdl script file and respond with wsdl contents.I was able to do with RaiseFault by pasting wsdl contents in it.But there is a catch, the support team can paste any wsdl but our code should change the soap address location in the wsdl to fixed hostname and respond back

endpoing sample of wsd:

<soap:address location="http://XXXX/WSDLs/XXX"/>

yes - well first, I would not use RaiseFault for that purpose, as it does not seem to be a fault. It's a correct, normal response. So instead I would recommend that you use AssignMessage.

You may want to keep the WSDL file separate, in which case I suggest you store it in the KVM. It's unfortunate that a policy is not able to retrieve a resource associated with the org (like a WSDL file etc). It would be a nicer model, I think. I'll see if we can add that feature.

The last thing you mentioned is an address replacement. For that I suggest you use JavaScript or XSLT to modify the WSDL file. XSLT might be easier since WSDL is all XML. There is just one, or maybe just a handful of locations you would need to modify. The XSLT would be perfect for that, and should be really simple.

Probably the "new" replacement address should also be stored in the KVM, and that value passed as a parameter to the XSLT.

We are now very very far from your original question, which is "how can I set a variable into the Message Context from JavaScript"? So if you have further questions, please ask a new question.

4420-ask-a-question-click-here.png