-
-
Notifications
You must be signed in to change notification settings - Fork 828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[REF] Catch Exceptions caused by CRM_Utils_Array::single() if there i… #32338
base: master
Are you sure you want to change the base?
[REF] Catch Exceptions caused by CRM_Utils_Array::single() if there i… #32338
Conversation
🤖 Thank you for contributing to CiviCRM! ❤️ We will need to test and review this PR. 👷 Introduction for new contributors...
Quick links for reviewers...
|
CRM/Custom/Page/CustomDataTrait.php
Outdated
])->single(); | ||
} | ||
catch (CRM_Core_Exception $e) { | ||
Civi::log()->warning('Tried loading Custom field data for Entity ' . $entity . ' id: ' . $id . ' and got the following message . ' $e->getMessage()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quote is in the wrong place. should be ' and got the following message: ' . $e->getMessage()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes me think that this has not been tested at all. This gives a parse error, even when the exception is not caught.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're currently running this patch in production (with the parse error corrected).
Prior to the implementation of this patch, we had many reports of "Expected one Address but found 0" occurring intermittently (as in, people who experienced this error upon loading a contact were able to load it without issue later in the day).
There has not been such a report since, and I can see quite a number of instances where the new logs appearing.
It's important to understand that we do not have any custom fields associated with Addresses or Emails, so the fact that this function simply returns with an empty array doesn't seem to have any impact on the operation of our site.
What does |
['id', '=', $id], | ||
], | ||
'checkPermissions' => TRUE, | ||
])->single(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is single()
still the correct method to call? It is meant for when you are sure that there is only a single result, but that you catch the exception seems to imply that this isn't always the case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that sometimes the array returned is empty, presumably due to a failed ACL check, not because there is actually no Address or Email. This leads the call to fail, which is now captured by the exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Sjord I feel like to keep the change as minimal as possible it makes sense to keep the code as is and just catch the exception as needed.
'checkPermissions' => TRUE, | ||
])->single(); | ||
} | ||
catch (CRM_Core_Exception $e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this exception be made more granular? That this only catches a NotASingleResultException or something like that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Sjord that would mean changing the exceptions thrown in CRM_Utils_Array::single but that might have changes required in other places. But also its probably worth catching any API exception which is generally a CRM_Core_Exception here anyway e.g. DB errors and the like.
CRM/Custom/Page/CustomDataTrait.php
Outdated
])->single(); | ||
} | ||
catch (CRM_Core_Exception $e) { | ||
Civi::log()->warning('Tried loading Custom field data for Entity ' . $entity . ' id: ' . $id . ' and got the following message . ' $e->getMessage()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes me think that this has not been tested at all. This gives a parse error, even when the exception is not caught.
@seamuslee001 can you explain any further how this line would come to retrieve no records? It does seem a bit odd - perhaps it is around when they don't actually have permission to see it? |
@eileenmcnaughton yeh @andrew-cormick-dockery seems to think there is some ACLs / cache at play I do note that the overall page permissions here https://github.com/civicrm/civicrm-core/blob/master/CRM/Contact/Page/View.php#L264 ignore the acl_contact_cache table all together but the APIv4 does in checkPermissions=True. I think AUG have been getting some ACL cache deadlocks. To my mind it is kind of a refactor of code. I will fix the syntax error and I just threw this up so ACD could try and deploy onto their D10 instance. |
…s either no record found for potential permission reasons
74787ac
to
a878301
Compare
OK - we can maybe wait & for a few days & see if more info surfaces ? |
I know AUG have put this into their production to at least mitigate the problems the users were having |
And it appears to be achieving its aims (so far at least). |
…s either no record found for potential permission reasons
Overview
It is possible that 0 records might be returned if permissions don't get checked or the cache is stale for some reason. I figure catching the exceptions thrown here https://github.com/civicrm/civicrm-core/blob/master/CRM/Utils/Array.php#L1218 is probably useful and logging them
Before
Potential for Expected to find one record but found nothing errors
After
Contact pages load
ping @eileenmcnaughton @andrew-cormick-dockery @johntwyman