Provide a default render element for errors or status messages (as main page content)

Created on 9 April 2024, 9 months ago

Problem/Motivation

Drupal handles PHP Exceptions and errors quite rough, typically with a white page just showing the text
"The website encountered an unexpected error. Try again later."

While it's discussable, if this is good or bad for regular users, it's good for security and not the point for this feature request.
Furthermore, it can be improved by solutions like https://www.drupal.org/project/error_page β†’

The point in this issue is, that it would be great, if Drupal Core could provide a standard render element type for errors to use in such cases.

Very much simplified example:

final class ExampleController extends ControllerBase {

  public function myExampleAction(): array {
    $build['content']  = [];
    try {
          $build['content'] = $this->buildMyExpectedResult();    
    }
    catch (ClientException $e) {
      $build['content'] = [
        '#type' => 'error', // This is an example of a standard render element. Unclear if it should be singular or plural.
        '#message' => $this->t('The remote could not be requested as expected. Please try again later'),
      ];
    }
    return $build;
  }
}

Due to the specific logics, for example clearing error messages in the queue I don't think StatusMessages status_messages would be the right choice here for the content. But of course it would be great to base them on the same base class for example!

See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21...

One key point is indeed that the existing status_messages element seems very opinionated in combination with \Drupal::messenger(): https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21...

In this issue, we don't want to show the error as message above the content, because the error message is the content!

Steps to reproduce

  1. Create a controller that should handle errors gracefully, for example because external sources are called that can fail.
  2. Add an exception handling by using try / catch
  3. Now it's your job to return a render array to show the (user-facing / modified) error message

The last point is where Drupal can and should help by providing a render array element to show errors in the page content consistently, so that there's a standard.

Proposed resolution

Implement a standard error message render element to use for such cases, extending RenderElement.

Remaining tasks

  1. Discuss
  2. Implement
  3. Test
  4. Release

User interface changes

None

API changes

Data model changes

None

Release notes snippet

✨ Feature request
Status

Active

Version

11.0 πŸ”₯

Component
RenderΒ  β†’

Last updated about 9 hours ago

Created by

πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

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

Comments & Activities

Production build 0.71.5 2024