Skip to main content
HaroonTyagi
Known Participant
October 19, 2014
Question

How to handle Coldfusion SOAP Web Service Errors

  • October 19, 2014
  • 20 replies
  • 4739 views

Hi,

I have just created simple wsdl example:

My Component:

<cfcomponent displayname="mytest">

    <cffunction name="service_login_authentication" access="remote" output="true" returntype="any" hint="Returns login">
     <cfargument name="login" type="string" required="yes">
        <cfargument name="password" type="string" required="yes">

          <cfif #arguments.login# eq "abcdef" and #arguments.password# eq "123456">
                  <cfxml variable="soapRes">                
                        <kps_response>
                            <message>OK</message>
                            <token>354dfdffsdf</token>
                         </kps_response>

                    </cfxml>
             <cfelse>
                     <cfthrow type="MyException" message="INVALID LOGIN" errorcode="1000" />
         </cfif>        
 
      <cfreturn soapRes >
    </cffunction>
   
</cfcomponent>

Its generating wsdl and no problem with response but when generating any error like username and password does not match then it's generating fault code like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<soapenv:Body>

      <soapenv:Fault>

         <faultcode>soapenv:Server.userException</faultcode>

         <faultstring>coldfusion.xml.rpc.CFCInvocationException: [coldfusion.runtime.CustomException : INVALID LOGIN. ]</faultstring>

         <detail>

            <ns1:hostname xmlns:ns1="http://xml.apache.org/axis/">HOST_NAME</ns1:hostname>

         </detail>

      </soapenv:Fault>

   </soapenv:Body>

</soapenv:Envelope>

But I want customize Fault Code like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<soapenv:Body>

      <soapenv:Fault>

         <faultcode>1000</faultcode>

         <faultstring>INVALID LOGIN</faultstring>

      </soapenv:Fault>

   </soapenv:Body>

</soapenv:Envelope>

Fault Code and Fault String should be customize and I don't want detail tag completely. In old ColdFusion version like ColdFusion 8 displaying detail tag with <ns1:stackTrace xmlns:ns1="http://xml.apache.org/axis/"> coldfusion.xml.rpc.CFCInvocationException: and so on.


Any suggestions on how to create customize faultcode and faultstring is very helpful.

Thanks!!


This topic has been closed for replies.

20 replies

BKBK
Community Expert
Community Expert
October 20, 2014

A suggestion:

<cfset var isLoginValid = False>

<cftry>

    <!--- Your code for testing login --->

    <cfif arguments.login eq "abcdef" and arguments.password eq "123456">

        <cfset isLoginValid = True>

    </cfif>

    <cfif isLoginValid>

        <cfxml variable="soapRes">

           <kps_response>

               <message>OK</message>

               <token>354dfdffsdf</token>

            </kps_response>

         </cfxml>

    <cfelse>

        <cfthrow type="MyException" message="INVALID LOGIN" errorcode="1000" />

    </cfif>

<cfcatch type="MyException">

    <cfxml variable="soapRes">

        <kps_response>

        <cfoutput><faultcode>#cfcatch.errorCode#</faultcode>

        <faultstring>#cfcatch.message#</faultstring></cfoutput>

        </kps_response>

    </cfxml>

</cfcatch>

</cftry>

<cfreturn soapRes>

HaroonTyagi
Known Participant
October 21, 2014

Thanks BKBK

I have tested in this way but in this case its generating fault code inside a response like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

   <soapenv:Body>

     <ns1:service_soap_loginResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://DefaultNamespace">

         <service_soap_loginReturn xsi:type="ns2:Document" xmlns:ns2="http://xml.apache.org/xml-soap">

            <kps_response>

               <faultcode>1000</faultcode>

               <faultstring>Invalid</faultstring>

             <kps_response>

         </service_soap_loginReturn>

      </ns1:service_soap_loginResponse>

   </soapenv:Body>

</soapenv:Envelope>

I want that Fault code should be inside Soap:Fault like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

   <soapenv:Body>

      <soapenv:Fault>

         <faultcode>1000</faultcode>

         <faultstring>INVALID_PASSWORD</faultstring>

      </soapenv:Fault>

   </soapenv:Body>

</soapenv:Envelope>

Any ideas?

BKBK
Community Expert
Community Expert
October 21, 2014

My suggestion just followed your example, presuming it works and expecting you to fill in the details. If I had to do it myself, I would:

- make the code more universal by using <cfsavecontent> in place of <cfxml> and returning string type.

- set the output attribute to false.

Something like this:

<cfcomponent output="false">

<cffunction name="service_login_authentication" access="remote" output="false" returntype="string" hint="Returns login">

<cfargument name="login" type="string" required="yes">

<cfargument name="password" type="string" required="yes">

<cfset var isLoginValid = False>

<cftry>

    <!--- Your code for testing login --->

    <cfif arguments.login eq "abcdef" and arguments.password eq "123456">

        <cfset isLoginValid = True>

    </cfif>

    <cfif isLoginValid>

        <cfsavecontent variable="soapRes"><?xml version="1.0" encoding="UTF-8"?>

           <kps_response>

               <message>OK</message>

               <token>354dfdffsdf</token>

            </kps_response>

         </cfsavecontent>

    <cfelse>

        <cfthrow type="MyException" message="INVALID LOGIN" errorcode="1000" />

    </cfif>

<cfcatch type="MyException">

    <cfsavecontent variable="soapRes">

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <soapenv:Body>

          <soapenv:Fault>

             <<cfoutput><faultcode>#cfcatch.errorCode#</faultcode>

                       <faultstring>#cfcatch.message#</faultstring></cfoutput>

          </soapenv:Fault>

       </soapenv:Body>

    </soapenv:Envelope

    </cfsavecontent>

</cfcatch>

</cftry>

<cfreturn soapRes>

</cffunction>

</cfcomponent>