Carbon: Exception: Unknown getter 'locale'

Hello,

I encountered an issue with the following code:

$holidays = ['0' => '2020-12-25', '1' => '2020-12-24']; //etc..
            
return $start_date->diffInDaysFiltered(function (Carbon $d) use ($holidays) {
   return $d->isWeekday() && !array_search($d->format("Y-m-d"), $holidays,true);
}, $end_date);

Carbon version: 2.41.0

PHP version: 7.3.5

I expected to get:

number of diff days between start_date and end_date

But I actually get:

Exception: Unknown getter 'locale'

With version 2.40.1 work!

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 25

Commits related to this issue

Most upvoted comments

You can try to pull the last commit on "nesbot/carbon": "dev-fix/issue-2217-mute-getter", I refactored to not use exception as it’s a good thing anyway in this case. So you should not longer have your debugger pausing with this change.

But:

my debugger paused on exception

We can’t consider this as an issue. Throwing an exception is not a bug, it tells “You can’t do that”.

This is not an anomaly. Here this is an expected exception. And try-catch is an existing PHP tool, so you should not expect all your included libraries not to use it. That’s meant for what its name literally describe: to try something.

Try to get “locale” => “You can’t do that” => OK, not a problem I have an alternative solution. And as long as it’s specific scoped exception types, this is actually library business user-land tools should not care about/interfere with.

So Carbon will fix this particular case on the next version, but be warned, similar caught exceptions may legitimately occur from other places and also from many other libraries we/you may include.