Skip to content

Commit

Permalink
Improved error handling for message search method (ddeboer#445)
Browse files Browse the repository at this point in the history
* Improved error handling for message search method

* added slash for constants

* Minor adjustments

* Style fixes

* Reverted to using imap_last_error to not alter error stack

* Removed unnecessary error description from exception message
  • Loading branch information
ikarol authored and Slamdunk committed Jan 28, 2020
1 parent fc09dbf commit 208a29f
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,13 @@ public function getMessages(ConditionInterface $search = null, int $sortCriteria
}
$messageNumbers = \imap_search(...$params);
}
if (false !== \imap_last_error()) {
// this way all errors occurred during search will be reported
throw new InvalidSearchCriteriaException(
\sprintf('Invalid search criteria [%s]', $query)
);
}
if (false === $messageNumbers) {
if (false !== \imap_last_error()) {
throw new InvalidSearchCriteriaException(\sprintf('Invalid search criteria [%s]', $query));
}

// imap_search can also return false
$messageNumbers = [];
}
Expand All @@ -203,13 +205,14 @@ public function getMessageSequence(string $sequence): MessageIteratorInterface
\imap_errors();

$overview = \imap_fetch_overview($this->resource->getStream(), $sequence, \FT_UID);
if (false !== \imap_last_error()) {
throw new InvalidSearchCriteriaException(
\sprintf('Invalid sequence [%s]', $sequence)
);
}
if (\is_array($overview) && [] !== $overview) {
$messageNumbers = \array_column($overview, 'uid');
} else {
if (false !== \imap_last_error()) {
throw new InvalidSearchCriteriaException(\sprintf('Invalid sequence [%s]', $sequence));
}

$messageNumbers = [];
}

Expand Down

0 comments on commit 208a29f

Please sign in to comment.