Score:0

How to show error on screen after failling a webservice call on Webform submitForm?

in flag

I made a custom handler for one of my webform to call a service when the form is submitted. Here is my code. It is working when the service return a response with status code = 200 but I cannot find a way to notify the view when an error is raised by the webservice. I would like to show a modal with an error message but cannot find how to do this.

class CRMWebformHandler extends WebformHandlerBase {

  public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
    try {
      $data = array(
        'firstName' => $webform_submission->getData()['lastname'],
        'lastName' => $webform_submission->getData()['firstname'],
        'civility' => $webform_submission->getData()['civility'],
        'email' => $webform_submission->getData()['email'],
      );
     
      $payload = json_encode($data);
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
      curl_setopt($curl, CURLOPT_POST, 1);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
      curl_setopt($curl, CURLOPT_URL, "https://my-url.com");
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

      $result = curl_exec($curl);
      curl_close($curl);

      $json = json_decode($result, TRUE);
      $succeed = is_bool($json) ? $json : FALSE;
      if (!$succeed && curl_getinfo($curl, CURLINFO_HTTP_CODE) !== 200) {
        throw new \Exception('No valid response from server.');
      }
    }
    catch (\Exception $exception) {
      \Drupal::messenger()->addError('No valid response from server.');
      return;
    }
  }
}
apaderno avatar
us flag
`curl_exec()` still returns `FALSE` in case of errors, even when you set `CURLOPT_RETURNTRANSFER` to `TRUE`. Passing to `json_decode()` a value that isn't a string is probably going to cause an error. Also, once the resource is de-allocated with `curl_close()`, passing that resource to any cURL function doesn't make sense.
in flag
I'll try to improve the code but this does not answer my current issue.
apaderno avatar
us flag
The code doesn't correctly handle the value returned from `curl_exec()`. This is at least the first part to fix.
in flag
With the current code, I have an exception thrown if the response status code is not 200 (No valid response from server) but still the view show a success message.
apaderno avatar
us flag
The code shown in the question catches the exception, but the exception is thrown when: The value of `$succeed` is equivalent to the Boolean `FALSE` and the value returned from `curl_getinfo($curl, CURLINFO_HTTP_CODE)` is different from 200. Calling `curl_getinfo($curl, CURLINFO_HTTP_CODE)` after `curl_close()` doesn't make sense. The exception is caught before it arrives to the view, which thinks nothing wrong happened.
apaderno avatar
us flag
Also, the question shows the code for a Webform handler. From it to a view the step is too long. There isn't any way for a Webform handler to return something to a view, which could also not be involved.
in flag
I move the `curl_close()` line after the if clause but it does not change anything. Throwing the exception cause an error saying `Uncaught PHP Exception Exception: "No valid response from server."`
in flag
So there is no way to notify the view an error happened on the handler ?
apaderno avatar
us flag
You should check what the Webform module code does and in which way it integrates with the Views module. If there is something the Views module asks to the Webform module, which the Webform module asks to its Webform handlers, then it's possible. Otherwise, no, it's not possible.
in flag
I could not find how to do it in the documentation, hence my question.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.