Drupal 9.2.7 on XAMPP 7.3.29 , 7.4.21 on Windows 10 Home.

I tried to create a custom module which allows global JavaScript and CSS (effective in all webpages in my website), by creating the directory root/modules/globalrs with the following files:


name: globalrs
type: module
core_version_requirement: ^8 || ^9
description: 'globalrs'
  - globalrs/globalrs


function globalrs_preprocess_page(&$variables) {
  $variables['#attached']['library'][] = 'globalrs/globalrs';


    behavior.js: {}
      style.css: {}


* {
    background: red;

My problem

My problem is that the website won't load, instead the following error appears:

The website encountered an unexpected error. Please try again later.

Even if I delete the module directory and flush all caches (flushing all cache_ tables as well as the cachetags table, and flushing the browser's cache, the website still has that error;
Since that error is generic, I don't know how to start to solve the problem.

A bug as a possible reason

It may be that the problem started after I have added $settings['rebuild_access'] = TRUE; to the end of /sites/default/settings.php, saved the file and then navigated in my web browser to localhost/example.com/core/rebuild.php, where I got the following error:

Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1044] Access denied for user 'example.com'@'localhost' to database 'example.com' in

Edit (removing the rest of the error messages because this is caused by an unrelated error while restoring a backup):

I believe that the problem happened sometime when I tried to restore a backup of the website to a new empty database with the same name which was registered as associated with the database user (or appeared to be such) but actually wasn't

Update - solution

I paste this here after Clive deleted my answer:

I revoked all database-specific privileges that the database user had on the database with the same name and then re-granted these database-specific privileges1 to that user; I could then access the website.

  • I swear I cannot understand any downvote on this post; a man is in trouble and seek some urgent help in humiliation; the feeling is already crappy, I don't find why to make it crappier. Drupal is created for website builders also, not only for Drupal programmers; these is the stance of the community ! Open drupal.org and you could read it.
    – Alkalix
    Nov 12, 2021 at 11:44
  • If you have access to the servers error log, that's where you would need to look to find the actual error message. Or even better if you are looking at that site on your local machine, configure PHP to display errors.
    – berliner
    Nov 12, 2021 at 12:12
  • 1
    One thing you can't do is enable a module and then delete it from the file system without disabling it first. This will just compound the issues. The actual error would be in the php or apache logs. You also don't have to flush caches with a sql client - you can do that from the admin. Something else is wrong here, which is why you need the XAMPP logs for php and apache.
    – Kevin
    Nov 12, 2021 at 13:06
  • Access denied for user 'example.com'@'localhost' to database 'example.com' Looks like the change you made to settings.php messedup the db connection. You could delete the settings.php file and let Drupal recreate it when you try to view the site. You'll get the instalation wizard as if you were doing a fresh install except since the DB is not empty, it will not get overriden.
    – No Sssweat
    Nov 12, 2021 at 15:23
  • @Kevin thanks, I didn't enable the module (from the "extend" tab) but maybe for custom modules it doesn't even needed?
    – Alkalix
    Nov 12, 2021 at 16:21

1 Answer 1


Before you create a custom module set

  • All messages, with backtrace information

in admin/config/development/logging. This option is available in Drupal core without any dev tools installed.

Then you get a more helpful error message if anything goes wrong.

With default

  • None

the error message gets replaced by "The website encountered an unexpected error. Please try again later.". The only purpose of this message is to hide the real error in production environments.

If it's too late and you can't open the admin UI anymore you can still enable all error messages in settings.php:

$config['system.logging']['error_level'] = 'verbose';

Don't remove the module code, this makes things worse. If you can't go forward or backwards, you can't fix the error with the more helpful error message and you can't uninstall the module because it is broken, then rollback the entire site from a backup or snapshot you've made before you've created the module.

  • Crap, I didn't create any snapshot before creating the module, rather, only before flushing all caches after finishing creating it... I didn't think mere files would effect anything.
    – Alkalix
    Nov 12, 2021 at 12:50
  • @Alkalix If you didn't do anything that affects the database in the module, then you could re-create a module with the same machine name and Drupal won't complain. Nov 12, 2021 at 15:38
  • @PatrickKenny yes I tried that... For some reason the site is still off, I will try what was suggested in the question comments.
    – Alkalix
    Nov 12, 2021 at 16:19
  • Clive deleted my answer which I wanted to mark as the most helpful because I strongly believe it would be most helpful (from the available answers here) to solve the described problem; what's going on here?...
    – Alkalix
    Nov 14, 2021 at 12:47
  • The question is Creating a custom module caused "The website encountered an unexpected error. Please try again later.". I'm sure you could have solved this by displaying a full error message, probably only a syntax error. The error you've encountered later after restoring the backup is unrelated. Restoring could be necessary for a large portion of the questions asked here if they are not resolved. In stack exchange you can not have two unrelated issues in one question.
    – 4uk4
    Nov 15, 2021 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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