Make DatabaseException extend \Throwable

Created on 7 February 2023, almost 2 years ago
Updated 10 September 2023, over 1 year ago

Problem/Motivation

When writing code to catch DatabaseException, intellisense doesn't see the existence of any exception-related methods.

Steps to reproduce

Write something like this:

namespace Drupal\Core\Database;

try {
  throw new DatabaseExceptionWrapper('Error message.');
}
catch (DatabaseException $e) {
  \Drupal::logger()->error($e->getMessage());
}

Then, since DatabaseException is just an empty interface and isn't guaranteed to have a getMessage() function, an IDE with intellisense will complain.

A workaround can be to either put /** @var \Throwable $e */ inside the catch, or to catch every specific class that implements the interface.

Proposed resolution

interface DatabaseException extends \Throwable { }

This still works with all the other existing code, and it makes it obvious to development tools that what you're catching is something that is a \Throwable.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Feature request
Status

Fixed

Version

10.1

Component
Database 

Last updated 2 days ago

  • Maintained by
  • 🇳🇱Netherlands @daffie
Created by

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

Comments & Activities

Production build 0.71.5 2024