0

I have a custom Webform Handler that submits a webform to an external SOAP api. Once a user submits the form, the data is sent to the external api, my custom webform hidden field 'error' is updated to read 'Yes' or 'No' depending on the feedback from the api and the form details are saved to the Drupal database. The contents of the custom 'error' field is then used to send a conditional email to the website admin. All works fine except when the SOAP api is offline, then there is an EntityStorageException error and the details in the form are neither saved to the database or submitted to the SOAP api. I've tried to Catch the EntityStorageException, but it's not working.

My code is as follows:

namespace Drupal\mycustom_handler\Plugin\WebformHandler;

use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\webformSubmissionInterface;
use Drupal\Core\Entity\EntityStorageException;
/**
 * Form submission handler.
 *
 * @WebformHandler(
 *   id = "sentinelapplication",
 *   label = @Translation("Sentinel Application"),
 *   category = @Translation("Form Handler"),
 *   description = @Translation("Administers application via SOAP"),
 *   cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_SINGLE,
 *   results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 * )
 */

class mycustom_handler extends WebformHandlerBase {
  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [];
  }
  /**
   * {@inheritdoc}
   */
  public function preSave(WebformSubmissionInterface $webform_submission) {
    $values = $webform_submission->getData(); 
    $endpoint = 'www.soapapi.com';

    // Add the SOAP Call
    try {
      global $base_url; 
      $wsdlurl = $base_url ."/modules/custom/mycustom_handler/src/Plugin/WebformHandler/mycustom.wsdl";  
      $client = new \SoapClient($wsdlurl, array('location'=>$endpoint, 'exceptions'=>true, 'trace'=>true));
      // upload webform detail to SOAP
      $webform_submission->setElementData('error', 'No');
    }
    catch (SoapFault $ex) {
      $webform_submission->setElementData('error', 'Yes');
    } 
    catch (Exception $ex) {
      $webform_submission->setElementData('error', 'Yes');
    } 
    catch (EntityStorageException $ex) {
      $webform_submission->setElementData('error', 'Yes');
    }
  }
}

I have also tried but had no luck with:

catch (\EntityStorageException $ex)

catch (Drupal\Core\Entity\EntityStorageException $ex)

catch (\Drupal\Core\Entity\EntityStorageException $ex)

Not sure if it is an issue with the preSave function (and postSave function as I tried that too) because if I use the submitForm function, the error does not display and the data is saved to the Drupal database. However, while using the submitForm function solves my error problem, I can't seem to figure out how to update the custom 'error' field in the submitForm function.

So has anyone got any idea how to Catch the EntityStorageException error or alternatively, if that is not possible, how can I update the webform submission within the submitForm function?

3
  • 2
    You are namespacing your code, have you tried catch (\SoapFault $ex)? (note the leading backslash) – Hudri Feb 10 '20 at 15:12
  • Can you post the full error and stack trace including line numbers? I suspect you're trying to catch it at the wrong point. It also seems strange to receive an EntityStorageException when you're using an external service. – imclean Feb 12 '20 at 4:18
  • Thanks Hudri - your solution solved the problem. Still trying to get my head around the correct formatting for namespacing. I had added the backslash to the SOAP call but forgot to add it to the Catch. – FrankDesign Feb 12 '20 at 9:40
0

Thanks to Hudri, the answer to this problem was to add a backslash before SoapFault in the catch because my try/catch code is made within a local namespace and the Soap call is made in the global namespace. So my code should have been catch (\SoapFault $ex). All working now - thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.