How to Debug - The Website Encountered an Unexpected Error. Please try again Later

By Rinku Jacob | 07th March 2022 | 2 min read

"The website encountered an unexpected error. Please try again later." this is the most common error encountered by a Drupal developer. Usually, errors are logged by Drupal and the logs can be accessed from Admin->Reports->Recent log messages. But when you get "The website encountered an unexpected error. Please try again later." you cannot even access the reports dashboard.

One option is to check the PHP/webserver error log as mentioned here

The easy way to get more information about the error is to set the error_level variable in settings.php file to verbose.

$config['system.logging']['error_level'] = 'verbose'; 
// hide|some|all|verbose

The error levels are defined in the bootstrap.inc as follows

/** * Error reporting level: display no errors. */
const ERROR_REPORTING_HIDE = 'hide';

/** * Error reporting level: display errors and warnings. */
const ERROR_REPORTING_DISPLAY_SOME = 'some';

/** * Error reporting level: display all messages. */
const ERROR_REPORTING_DISPLAY_ALL = 'all';

/** * Error reporting level: display all messages, plus backtrace information. */
const ERROR_REPORTING_DISPLAY_VERBOSE = 'verbose';

In my case, got this message after enabling the verbose mode.

The website encountered an unexpected error. Please try again later.
PDOException: SQLSTATE[HY000] [2002] Connection refused in Drupal\Component\DependencyInjection\PhpArrayContainer->createService() (line 79 of /app/web/core/lib/Drupal/Component/DependencyInjection/PhpArrayContainer.php).

Drupal\Component\DependencyInjection\PhpArrayContainer->createService(Array, 'database') (Line: 176)
Drupal\Component\DependencyInjection\Container->get('database', 1) (Line: 212)
Drupal\Component\DependencyInjection\PhpArrayContainer->resolveServicesAndParameters(Array) (Line: 62)
Drupal\Component\DependencyInjection\PhpArrayContainer->createService(Array, 'cache.container') (Line: 176)
Drupal\Component\DependencyInjection\Container->get('cache.container') (Line: 549)
Drupal\Core\DrupalKernel->getCachedContainerDefinition() (Line: 894)
Drupal\Core\DrupalKernel->initializeContainer() (Line: 472)
Drupal\Core\DrupalKernel->boot() (Line: 707)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

 Which easily pointed me to the incorrect database name :-)

Note: It is not recommended to make the error log detail in a production environment. Make sure that you use settings.local.php to add $config['system.logging']['error_level']. See sites/example.settings.local.php

Reference

https://drupal.stackexchange.com/questions/127182/how-do-i-enable-developer-debug-mode