14 Replies Latest reply: Oct 22, 2014 3:16 AM by BKBK RSS

    How to handle Coldfusion SOAP Web Service Errors

    HaroonTyagi Community Member

      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!!

       


        • 1. Re: How to handle Coldfusion SOAP Web Service Errors
          BKBK Community Member

          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>

          • 2. Re: How to handle Coldfusion SOAP Web Service Errors
            HaroonTyagi Community Member

            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?

            • 3. Re: How to handle Coldfusion SOAP Web Service Errors
              BKBK Community Member

              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>

              • 4. Re: How to handle Coldfusion SOAP Web Service Errors
                HaroonTyagi Community Member

                Many Thanks BKBK!!

                 

                I already check this. When calling GetSOAPResponse it just take the whole cfsavecontent string inside 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://wsdl.research">

                        <service_soap_loginReturn xsi:type="xsd:string">

                <![CDATA[<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>]]>

                </service_soap_loginReturn>

                      </ns1:service_soap_loginResponse>

                   </soapenv:Body>

                </soapenv:Envelope>

                 

                Any ideas?

                • 5. Re: How to handle Coldfusion SOAP Web Service Errors
                  BKBK Community Member

                  HaroonTyagi wrote:

                   

                  I already check this.

                  ...

                  ...

                  Any ideas?

                  I don't understand what you mean. Are you asking a new question? Did my last suggestion work or not?

                  • 6. Re: How to handle Coldfusion SOAP Web Service Errors
                    HaroonTyagi Community Member

                    I am saying that your last suggestion I have checked but not working.

                     

                    From your last suggestion the output of fault is :

                    <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://wsdl.research">

                            <service_soap_loginReturn xsi:type="xsd:string">

                    <![CDATA[<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>]]>

                    </service_soap_loginReturn>

                          </ns1:service_soap_loginResponse>

                       </soapenv:Body>

                    </soapenv:Envelope>

                     

                    But the output should be as below:

                     

                    <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>

                    • 7. Re: How to handle Coldfusion SOAP Web Service Errors
                      BKBK Community Member

                      The component you are using as web service is probably different from mine.

                      • 8. Re: How to handle Coldfusion SOAP Web Service Errors
                        HaroonTyagi Community Member

                        I am using same component which you given:

                         

                        <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>

                         

                        I did not change anything. if you have another component can you post here so I can check if I am doing something wrong may be.

                        • 9. Re: How to handle Coldfusion SOAP Web Service Errors
                          BKBK Community Member

                          HaroonTyagi wrote:

                           

                          I am saying that your last suggestion I have checked but not working.

                           

                          But the output should be as below:

                           

                          <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>

                          This is exactly the result that my suggestion produces. I have just checked it myself.

                          • 10. Re: How to handle Coldfusion SOAP Web Service Errors
                            BKBK Community Member

                            Your code might be invoking a cached, hence old, version of the WSDL. Do something like the following to make sure the current WSDL is invoked:

                             

                            <cfset argStruct.refreshWSDL="yes">

                            <cfset ws=createobject("webservice","http://127.0.0.1:8500/workspace/myServices/soapTest.cfc?wsdl",argStruct)>

                            <cfdump var="#ws.service_login_authentication('abc','123456')#">

                            • 11. Re: How to handle Coldfusion SOAP Web Service Errors
                              HaroonTyagi Community Member

                              But my component is not produces this fault code:

                              <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>


                              What is your ColdFusion Version as i have checked in ColdFusion 11 developer edition and ColdFusion 8 enterprise which is on window 2003 server.

                              Could you please check my below component, did i am doing something wrong.

                              <cfcomponent output="false">

                                  <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">
                                <cfset var isLoginValid = False>
                                      <cftry>
                                        <cfif #arguments.login# eq "1111" and #arguments.password# eq "1111">
                                            <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>

                               

                              If my component is okay then may be possible some setting in ColdFusion administrator. i have checked in SoapUI as well via getSoapResponse method.

                              • 12. Re: How to handle Coldfusion SOAP Web Service Errors
                                HaroonTyagi Community Member

                                thanks BKBK, it's working.

                                 

                                I was calling like this:

                                <cfscript>

                                ws = CreateObject("webservice","http://localhost/CFTest/MainLogin.cfc?wsdl", {refreshWSDL=true});

                                ws.service_login_authentication("1111","1111");

                                req = GetSOAPResponse(ws);

                                writeoutput(req);

                                </cfscript>

                                • 13. Re: How to handle Coldfusion SOAP Web Service Errors
                                  HaroonTyagi Community Member

                                  It is possible I can get this fault output from GetSOAPResponse(webservice)  because I just want to pass from CFC main fault code and main response, I don't want to pass complete soap xml.

                                  • 14. Re: How to handle Coldfusion SOAP Web Service Errors
                                    BKBK Community Member

                                    I would strive to keep the web service as simple and as universal as possible. That often means passing and returning simple types.

                                     

                                    This is especially relevant because Coldfusion is weakly typed. What it calls 'any' or 'xml' type may be interpreted quite differently by other platforms. That is what my suggestion has been all about.

                                     

                                    You should either accept what Coldfusion's GetSOAPResponse(webservice) is giving you or you should craft the SOAP response you want, as I did in my suggestion, and return it as a string.