DataPower

DataPower

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
Expand all | Collapse all

set return code when xslt call dp:gatewayscript

  • 1.  set return code when xslt call dp:gatewayscript

    Posted Tue April 07, 2020 09:56 AM
    ​Hello

    I understand it's possible to call  a Gateway script from an XSLT stylesheet
    So i use the command below (where $JsonString  is a json)

    <xsl:variable name="flux" select="dp:gatewayscript('ChequeSansCoupon.js', $JsonString , true(), '')"/>


    If $JsonString  is not json, how code into ChequeSansCoupon.js a json error to have this return below


    <gatewayscript>
    	<errorcode>0x0000200</errorcode>
    </gatewayscript>


    Thanks for reply



    ------------------------------
    David Leroux
    ------------------------------


  • 2.  RE: set return code when xslt call dp:gatewayscript

    Posted Wed April 08, 2020 05:33 AM
    Edited by Hermann Stamm-Wilbrandt Wed April 08, 2020 05:40 AM

    Hi,

    > If $JsonString is not json, how code into ChequeSansCoupon.js a json error to have this return below

    >

    in ChequeSansCoupon.js:

    session.input.readAsJSON(function (error, jsonstring) {
        if (error) {
            session.reject("<gatewayscript><errorcode>0x0000200</errorcode></gatewayscript> ");
            return;
        }
    
    ...
    });
    


    ------------------------------
    Hermann Stamm-Wilbrandt
    Compiler Level 3 support & Fixpack team lead
    IBM DataPower Gateways (⬚ᵈᵃᵗᵃ / ⣏⠆⡮⡆⢹⠁⡮⡆⡯⠂⢎⠆⡧⡇⣟⡃⡿⡃)
    ------------------------------



  • 3.  RE: set return code when xslt call dp:gatewayscript

    Posted Wed April 08, 2020 05:50 AM
    ​thanks Herman.
    I Don't understand when you said "you cannot, because in case of JSON parse error the GatewayScipt does not get executed at all"

    I have the impression that my script is well executed even if the message is not json

    for example, here is ChequeSansCoupin.js

    session.input.readAsJSON(function (readAsJSONError, jsonData) {
        if (readAsJSONError) {
    		  session.output.write('false');
       } else {
    	  session.output.write('true');
         
    }});

    when i call gatewayscript with a non json message, the response is

    <gatewayscript>
    	<errorcode>0x00000000</errorcode>
    	<result>false</result>
    </gatewayscript>


    So for my case, i need to test /gatewayscript/result = 'false'. But I would have liked to test /gatewayscript/errorcode <> '0x00000000'

    So you think it's not possible to change errorcode  into a gatewayscript ?




    ------------------------------
    David Leroux
    ------------------------------



  • 4.  RE: set return code when xslt call dp:gatewayscript

    Posted Wed April 08, 2020 05:55 AM

    You cite my incorrect first response posting, which I edited afterwards.

    The corrected response allows to do error handling in GatewayScipt with the help of session.reject().



    ------------------------------
    Hermann Stamm-Wilbrandt
    Compiler Level 3 support & Fixpack team lead
    IBM DataPower Gateways (⬚ᵈᵃᵗᵃ / ⣏⠆⡮⡆⢹⠁⡮⡆⡯⠂⢎⠆⡧⡇⣟⡃⡿⡃)
    ------------------------------



  • 5.  RE: set return code when xslt call dp:gatewayscript

    Posted Wed April 08, 2020 06:11 AM
    If I do this, the WSP, which use the dp:gatewayscript thrown command, generate a error Policy.

    If it's not possible to set a errorCode into ChequeSansCoupon without generate a global error Policy, i will continue to use this


    if (readAsJSONError) {
    		  session.output.write('false');


    Thanks a lot Herman :-)




    ------------------------------
    David Leroux
    ------------------------------



  • 6.  RE: set return code when xslt call dp:gatewayscript

    Posted Wed April 08, 2020 12:22 PM

    The error code returned is not one that you have control over.  I'm pretty sure session.reject would have one error code, an uncaught exception would may have a different error code depending upon the issue, etc.  The error code is dependent upon the type of error encountered by the .js.  With no runtime errors or a session.reject, I would anticipate an error code returned as x00000000.

    Regards,

    Steve




  • 7.  RE: set return code when xslt call dp:gatewayscript

    Posted Wed April 08, 2020 01:09 PM
    ​Hello Steve
    I agree with you   With no runtime errors or a session.reject, I would anticipate an error code returned as x00000000

    But session.reject (if string is not json, for example), returns a error into the WSP which throws an error Policy (that's not what i want). I just want to recover the xml returns with dp:gatewayscript without the WSP generating an error policy

    ------------------------------
    David Leroux
    ------------------------------



  • 8.  RE: set return code when xslt call dp:gatewayscript

    Posted Thu April 09, 2020 07:31 AM
    You can set return-error to false and generate the error response yourself. 
    If you change the GatewayScript call as follows:
    <xsl:variable name="flux" select="dp:gatewayscript('ChequeSansCoupon.js', $JsonString , false(), '')"/>
    and then inside ChequeSansCoupon.js change the "catch" block to something like the one below:
    session.input.readAsJSON(function (readAsJSONError, jsonData) {
        if (readAsJSONError) {
    		var errorResponse = '<gatewayscript><errorcode>0x0000200</errorcode></gatewayscript>';
            session.output.write(XML.parse(errorResponse));
        } else {
    		var response = '<gatewayscript><result>Input string was a valid JSON string</result></gatewayscript>'
            session.output.write(XML.parse(response));
        }
    });
    you'll get the following back to $flux if $JsonString is not valid:
    <gatewayscript>
       <errorcode>0x0000200</errorcode>
    </gatewayscript>
    If the string is valid, then you'll get:
    <gatewayscript>
       <result>Input string was a valid JSON string</result>
    </gatewayscript>

    Hope this helps.



    ------------------------------
    Hermanni Pernaa
    ------------------------------



  • 9.  RE: set return code when xslt call dp:gatewayscript

    Posted Thu April 09, 2020 09:51 AM
    ​hi Hermanii
    Nice ! Thanls
    It's the same than test my gatewayscript return string with 'false' :-)
    But your solution is pretty much better
    thanks

    ------------------------------
    David Leroux
    ------------------------------



  • 10.  RE: set return code when xslt call dp:gatewayscript

    Posted Thu April 09, 2020 09:39 AM

    Hi David,

    Since you're handling the error code in the xslt, you could do a

    <dp:accept/>

    to allow your WSP processing rule to continue.  The session.reject in GatewayScript or <dp:reject> in XSLT, unless you're in strict mode which you probably are not, will continue executing your xslt to completion and will then cause your processing rule to stop, but the <dp:accept/> will undo the current reject.

    Regards,

    Steve




  • 11.  RE: set return code when xslt call dp:gatewayscript

    Posted Thu April 09, 2020 09:54 AM
    ​Hi Steve and Stephen :-)
    Thanks for reply
    I will test it immediately

    ------------------------------
    David Leroux
    ------------------------------