I'm using a custom submit handler for a Webform, which POSTs important form data to another API. That part works, but on occasion there may be random network errors that prevent a successful submission, such as if the REST endpoint happened to be down at that particular moment (or whatever).
In those cases, I don't want the user to think their form successfully submitted when it didn't. How can I override the confirmation page with a custom error page? I tried drupal_set_message()
, but it doesn't show up anywhere on the confirmation page that I could see.
Ideally, I would also be able to get back the event ID generated by watchdog
logging so that the user, if they contact us by phone or email upon seeing the error, would be able to provide more information that can help us debug it.
So in summary, in a perfect world, a failed submission would result in:
- The form submission ($form_state) being logged into watchdog or another log
- Watchdog returns the associated event ID
- The user sees a custom error page describing the error, with the event ID as an error code to help in debugging.
I can hack something together with die()
and a hash of the form data as an error code, but is there a better, proper Drupal way?
Relevant code so far:
try {
$apiResponse = drupal_http_request($apiEndPoint, [
'headers' => ['Content-Type' => 'application/json'],
'method' => 'POST',
'data' => drupal_json_encode($formData),
'timeout' => 5 // Timeout in seconds.
]);
// I'm not okay :(
if ($apiResponse->code != "200") {
throw new Exception ($apiResponse->data, $apiResponse->code);
}
} catch (Throwable $e) {
watchdog('tfm_custom_form_handlers', "A custom form handler failed: (@code) @message.", [
'@code' => $e->getCode(),
'@message' => $e->getMessage(),
], WATCHDOG_ERROR);
}
Thanks for the help!