Question

I have created Magento Custom V2 API it's working fine, but the fault code is returning wrong.

My Code is

api.xml

<?xml version="1.0"?>
<config>
    <api>
        <resources>
            <test_customer translate="title" module="test">
                <title>Custom Customer API</title>
                <model>test/customer_api</model>
                <acl>test/customer</acl>
                <methods>
                    <multiCreate translate="title" module="test">
                        <title>Multiple customer creation</title>
                    </multiCreate>
                </methods>
                <faults module="test">
                    <data_invalid>
                        <code>C100</code>
                        <message>Invalid customer data. </message>
                    </data_invalid>
                    <filters_invalid>
                        <code>C101</code>
                        <message>Invalid filters specified. </message>
                    </filters_invalid>
                    <not_exists>
                        <code>C102</code>
                        <message>Customer not exists.</message>
                    </not_exists>
                    <not_deleted>
                        <code>C103</code>
                        <message>Customer not deleted. </message>
                    </not_deleted>
                    <no_data>
                        <code>C104</code>
                        <message>No/Invalid Data was found. </message>
                    </no_data>
                    <not_array>
                        <code>C105</code>
                        <message>Input should be an Array. </message>
                    </not_array>
                    <group_not_exists>
                        <code>C106</code>
                        <message>Customer sub group not exists.</message>
                    </group_not_exists>
                    <additional_attributes>
                        <code>C107</code>
                        <message> Invalid additional attributes data. </message>
                    </additional_attributes>
                    <customer_exist>
                        <code>C108</code>
                        <message>Customer Exists.</message>
                    </customer_exist>
                    <api_limit>
                        <code>APIL100</code>
                        <message>API Limit is exceeded.</message>
                    </api_limit>
                </faults>
            </test_customer>
        </resources>

        <resources_alias>
            <customer>test_customer</customer>
        </resources_alias>

       <v2>
            <resources_function_prefix>
                <customer>testCustomer</customer>
            </resources_function_prefix>
        </v2>

         <acl>
           <resources>
               <test translate="title" module="test">
                   <title>test</title>
                   <sort_order>1</sort_order>
                   <customer translate="title" module="test">
                       <title>Customer</title>
                       <sort_order>2000</sort_order>
                       <multiCreate translate="title" module="test">
                           <title>multiCreate</title>
                       </multiCreate>
                   </customer>
               </test>
           </resources>
       </acl>

    </api>
</config>

V2.php

<?php

class Hello_Test_Model_Customer_Api_V2 extends Hello_Test_Model_Customer_Api{


    public function multiCreate($customerData){

       try{

           $email = $customerData->email;
           $customers = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('email');

            foreach($customers as $customers_Data){
                $customer_email = $customers_Data->getEmail();
                if($customer_email == $email){
                    $this->_fault('customer_exist','Customer is Exists.');
                }
            }


       }catch (Exception $ex){
           $this->_fault('data_invalid',$ex->getMessage());
       }

        return $customer->getId();

    }


}

here i'm validating customer by E-Mail & throwing exception correct but it's not returing original fault message?

result is

SoapFault exception: [C100] customer_exist

Actual Error Code is C108 & Message is Customer is exists

Please suggest me if i went wrong?

Was it helpful?

Solution

$this->_fault() throws a Mage_Api_Exception exception. Because this is within your try..catch block, you catch this Mage_Api_Exception with catch (Exception $ex) where you throw a different Mage_Api_Exception with the same message but the data_invalid key.

What you want to do instead is only catch exceptions which are not of the Mage_Api_Exception type. I would suggest catching and rethrowing Mage_Api_Exception like this, and use your current catch block for all other exceptions:

   } catch (Mage_Api_Exception $ex) {
       throw $ex;
   } catch (Exception $ex) {
       $this->_fault('data_invalid', $ex->getMessage());
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top