Never let the blank page with "The website encountered an unexpected error" be shown, especially when customer is waiting for payment confirmation.

Created on 18 January 2024, 11 months ago
Updated 19 January 2024, 11 months ago

Sometimes exceptions other than PaymentGatewayExceptions can occur in PaymentProcess->buildPaneForm(). Or even PHP errors. In that case, they are never catched and result in a blank page The website has encountered an error.

IMHO this results in a horrible user experience. As a client, I would feel very uncomfortable trying to buy anything else on a website that would handle errors that way.

For my client website, I have overridden PaymentProcess in order to catch any errors that can occur and try to display a relevant message to the customer. The key ideas being to not display a blank page as if the whole site had broken down ; but also to let the customer know that at this step his/her payment could potentially already have been successful. So we don't want him/her to try again before we can take the time to check what went wrong.

class MyModulePaymentProcessPane extends PaymentProcess {

  public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
    try {
      return parent::buildPaneForm($pane_form, $form_state, $complete_form); 
    } catch (\Throwable $t) {
      if ($t instanceof NeedsRedirectException) {
        throw $t; // we don't want to block NeedsRedirectExceptions because they are responsible for redirecting the customer
      }
      Utils::logThrowableTracedMessage($t); // this is some custom function that logs the whole Stack Trace. Also the admin receives an email alert when an error is logged so we can work on it right away.
      \Drupal::messenger()->addError("A message that lets the customer know that at this step his/her payment could potentially already have been successful. So we don't want him/her to try again before we can take the time to check what went wrong");
      throw new NeedsRedirectException(Url::fromRoute('my_module.error_page')->toString());
    }
  }

}

I think this is particularly important at this step because the customer has entered his/her payment information and is waiting for confirmation. But more generally I think we should never display a blank page with The website encountered an error. So I would suggest we also make sure this never happens in any situation inside the whole Drupal / Commerce experience.

✨ Feature request
Status

Active

Version

2.37

Component

User experience

Created by

πŸ‡«πŸ‡·France nicolas bouteille

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @nicolas bouteille
  • πŸ‡ΊπŸ‡¦Ukraine abramm Lutsk

    Probably something like https://www.drupal.org/project/error_page β†’ could be used instead.
    There are a lot of places in Drupal, Commerce, or any other software where unexpected errors may occur.
    Patching just a payment process pane seems to be a very very very specific fix for a specific edge case.

  • πŸ‡«πŸ‡·France nicolas bouteille

    Thanks I did not know about this module. I'll go check it out.
    With such a module, I would agree Drupal Commerce does not need to make sure they handle errors better than now.
    Maybe we could just make a recommendation on the Commerce's project page to invite developers starting to use Commerce to better accompany the customers in case an error occurs instead of a blank page.
    However, I still believe that an error happening after the customer has entered his/her payment details and is waiting for payment confirmation is a very very very critical moment that deserves a specific reassuring message. So I will probably still handle this specific use case with special care as far as I'm concerned.

Production build 0.71.5 2024