Skip to content

Commit

Permalink
Catch exception generated when senator has no associated user
Browse files Browse the repository at this point in the history
This commit was pushed using -n due to a number of pre-existing
errors in code style.  Those issues will be resolved with a future
commit.
  • Loading branch information
routinet committed Jan 16, 2025
1 parent e1bbef9 commit ca64222
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions web/modules/custom/nys_bills/src/Form/BillForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,20 +535,37 @@ public function hasThread($entity_id) {
if (!$this->currentUser->isAuthenticated()) {
return FALSE;
}
$user_storage = $this->entityTypeManager->getStorage('user');
// Need the global user object.
$user = $user_storage->load($this->currentUser->id());
$senator = $this->nysUserHelper->getSenator($user);

$messages = NULL;
if ($senator) {
$messages = $this->entityTypeManager->getStorage('private_message')->loadByProperties(
$messages = [];
try {
$user_storage = $this->entityTypeManager->getStorage('user');
// Need the global user object.
$user = $user_storage->load($this->currentUser->id());
$senator = $this->nysUserHelper->getSenator($user);

if ($senator) {
$messages = $this->entityTypeManager->getStorage('private_message')
->loadByProperties(
[
'field_to' => $senator->field_user_account->target_id ?? [],
'owner' => $user->id(),
'field_bill' => $entity_id,
]
);
);
}
}
catch (\Throwable) {
// The above query can throw an exception if the senator does not have
// a valid user account. In that event, log an alert.
$id = isset($senator) ? $senator->id() : 'No senator loaded';
$name = isset($senator) ? $senator->getName() : 'Name not available';
$this->logger('nys_bills')->error(
'Failed to find an associated user for a senator (%id : @name)',
[
'%id' => $id,
'@name' => $name,
]
);
}

$thread = NULL;
Expand Down

0 comments on commit ca64222

Please sign in to comment.