Fixed crashing on date formatting helper when value is not actually a date #11249
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This just wraps the Carbon method in a try/catch to prevent crashing on bad data, so if the $date value isn't actually valid, we don't crash out completely.
While this shouldn't typically happen since we validate dates before entering them into the database (and we use date/datetime fields for native fields in the system), it is a possible scenario that a custom field could be created as an "ANY" field, data gets added, and then the custom field format gets edited to DATE or DATETIME later. If someone put bad data in the database before then - or if they manually edited the field's value - it will crash with:
This will now not crash and will instead return an error that the date is invalid.
While I find it ugly to return a string (instead of false, etc), this method was flat-out crashing as it is now (when there was bad data). A further refractor might be in order down the line, but this at least fixes the immediate problem.