Remove the try/catch block from DateTimeComputed::getValue()

Created on 13 January 2019, almost 6 years ago
Updated 7 February 2023, almost 2 years ago

Problem/Motivation

DateTimeComputed::getValue() has this code:

    catch (\Exception $e) {
      // @todo Handle this.
    }

This swallows the exception thrown if the computed field property's value can't be derived.

For instance, if you have a date or date range field that you've given bad data to, and then try to get the computed value, you just get NULL, with no explanation why.

The try/catch was added in #1844956: Optimize date formatting performance → without much discussion. (See Comment #34.) Probably is was a work-around to get tests to pass. The patch in Comment #23 removes the try/catch block, and it initially failed.

Then #3240181: \Drupal\datetime\DateTimeComputed::getValue() causes deprecations on PHP 8.1 → added a check for NULL a few lines before the try/catch block, and a re-test of #23 passed (Comment #30).

Proposed resolution

Remove the try/catch block.

Remaining tasks

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

N/A

📌 Task
Status

Needs work

Version

10.1 ✨

Component
Base  →

Last updated about 3 hours ago

Created by

🇬🇧United Kingdom joachim

Live updates comments and jobs are added and updated live.
  • Needs change record

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States benjifisher Boston area

    I want to consider it some more, but I think that removing the try/catch block (as in #23) is the right thing to do.

    I do not see any discussion here of when/why the try/catch block was added, so I did a little Git research. It was added in #1844956: Optimize date formatting performance → without any discussion. It is a little hard to find, since the code was moved in #2083785: Add support for determining which field properties are cacheable → .

    Unfortunately, I have to set the status back to NW for a really minor point. When we remove the try/catch block and reindent the code, we have to re-wrap the comments to 80 characters. That is a novice task.

  • Assigned to sourabhjain
  • 🇮🇳India sourabhjain

    I am working on point which is mentioned in #34.

  • Issue was unassigned.
  • Status changed to Needs review almost 2 years ago
  • 🇮🇳India sourabhjain

    I have re-wrap the comments to 80 characters. Please review.

  • Status changed to RTBC almost 2 years ago
  • 🇬🇧United Kingdom jonathanshaw Stroud, UK
  • 🇺🇸United States benjifisher Boston area

    @sourabhjain: Thanks for updating the patch. Next time, please remember to remove the Novice tag and "cross off" the task from the issue summary. I am doing that now.

    The patch in #36 also breaks the long line (originally the first line of the try block). +1 for that and +1 for RTBC.

    If I have a chance later, I will rewrite the issue summary. I would like to save the next reviewer from having to read all the comments.

    I think this issue is more of a task than a bug report, so I am changing the category.

  • 🇺🇸United States benjifisher Boston area
  • 🇺🇸United States benjifisher Boston area

    I am updating the issue summary, using the standard headings.

    Probably the try/catch was added in #1844956: Optimize date formatting performance → in order to get tests to pass. If anyone is curious, they can look through the failing tests and the interdiffs on that issue to figure out exactly which test was failing. Probably the test for NULL added in #3240181: \Drupal\datetime\DateTimeComputed::getValue() causes deprecations on PHP 8.1 → was the right fix all along.

    The tests in Drupal core all pass, for either the patch in #23 or the one in #36. But custom or contrib modules might have problems when we let any remaining exceptions bubble up, so we should probably not back-port this change to 10.0.x.

  • 🇺🇸United States benjifisher Boston area

    Let's update the title to match what we are actually doing.

  • Status changed to Needs work almost 2 years ago
  • 🇬🇧United Kingdom catch

    It seems possible that someone could have committed broken code, never fixed it, and that code could then start throwing exceptions once the site updates to the version that has this in. Good that they'll find out, but could be disruptive.

    I think that might be OK in a minor, however we should add a release note and change record (previously swallowed errors will now throw an exception).

    Also should the method phpdoc be updated to include @throws?

    Will get a second opinion from another committer on whether we need to go further and log the exception/trigger_error() before making a hard fail in Drupal 11.

  • 🇬🇧United Kingdom longwave UK

    Re #42 it's also possible someone is relying on the current NULL return to detect that a date cannot be parsed. This is effectively a change in return value and as per policy we should not change these if possible → . In this case we can notify users with @trigger_error that this behaviour will change in future?

    I also note that \Drupal\Core\Datetime\Element\Datetime::valueCallback() has similar code:

          try {
            $date_time_format = trim($date_format . ' ' . $time_format);
            $date_time_input = trim($date_input . ' ' . $time_input);
            $date = DrupalDateTime::createFromFormat($date_time_format, $date_time_input, $element['#date_timezone']);
          }
          catch (\Exception $e) {
            $date = NULL;
          }
    

    Should we consider changing this one as well?

    This issue appears to stem from the fact that PHP's \DateTime::createFromFormat() does not throw exceptions and returns FALSE on failure, but our DateTimePlus changes this to use exceptions; however in some cases we just revert that behaviour back to the PHP way.

  • 🇬🇧United Kingdom catch

    In this case we can notify users with @trigger_error that this behaviour will change in future?

    Yeah I'm leaning this way too, although I wonder if we should do trigger_error('...', E_USER_WARNING); so that people actually get an actionable error message in Drupal 10, which can then be an exception in Drupal 11, rather than a silenced deprecation.

  • Status changed to Needs review almost 2 years ago
  • Status changed to Needs work almost 2 years ago
  • 🇬🇧United Kingdom longwave UK

    @Ranjit1032002 thanks for the patch but it does not apply - it also looks incorrectly formed.

    I think the next steps here are to discard the patch so far and change the todo to a trigger_error(), so we can notify users that this behaviour will change. We also need a change record documenting this.

Production build 0.71.5 2024