Score:0

Creating a custom module caused "The website encountered an unexpected error. Please try again later."

mu flag

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:

globalrs.info.yml

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

globalrs.module

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

globalrs.libraries.yml

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

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.

mu flag
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.
berliner avatar
bd flag
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.
Kevin avatar
in flag
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.
No Sssweat avatar
ua flag
`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.
mu flag
@Kevin thanks, I didn't enable the module (from the "extend" tab) but maybe for custom modules it doesn't even needed?
mu flag
@NoSssweat I just tried that and I get, in the installation wizard: `Failed to connect to your database server. The server reports the following message: SQLSTATE[HY000] [1044] Access denied for user 'example.com'@'localhost' to database 'example.com'.`
mu flag
Okay problem solved; I can access the website; I wrote in an answer what I did.
mu flag
Can someone explain why Clive deleted my answer?...
Score:2
cn flag

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.

mu flag
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.
cn flag
@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.
mu flag
@PatrickKenny yes I tried that... For some reason the site is still off, I will try what was suggested in the question comments.
mu flag
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?...
4uk4 avatar
cn flag
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.
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.