imap error exception

This commit is contained in:
Yuri Kuznetsov
2024-05-10 14:35:26 +03:00
parent caa536f17a
commit ed77c8758c
8 changed files with 93 additions and 12 deletions

View File

@@ -35,6 +35,7 @@ use Espo\Core\Mail\Account\Storage\Params as StorageParams;
use Espo\Core\Controllers\Record;
use Espo\Core\Api\Request;
use Espo\Core\Mail\Exceptions\ImapError;
class InboundEmail extends Record
{
@@ -45,7 +46,8 @@ class InboundEmail extends Record
/**
* @return string[]
* @throws \Espo\Core\Exceptions\Error
* @throws Error
* @throws ImapError
*/
public function postActionGetFolders(Request $request): array
{

View File

@@ -32,6 +32,8 @@ namespace Espo\Core\Mail\Account;
use Espo\Core\Exceptions\Error;
use Espo\Core\Mail\Account\Storage\Flag;
use Espo\Core\Mail\Exceptions\ImapError;
use Espo\Core\Mail\Exceptions\NoImap;
use Espo\Core\Mail\Importer;
use Espo\Core\Mail\Importer\Data as ImporterData;
use Espo\Core\Mail\ParserFactory;
@@ -69,6 +71,8 @@ class Fetcher
/**
* @throws Error
* @throws ImapError
* @throws NoImap
*/
public function fetch(Account $account): void
{

View File

@@ -36,6 +36,8 @@ use Espo\Core\Mail\Account\Fetcher;
use Espo\Core\Mail\Account\Storage\Params;
use Espo\Core\Mail\Account\StorageFactory;
use Espo\Core\Mail\Exceptions\ImapError;
use Espo\Core\Mail\Exceptions\NoImap;
use Espo\Core\Utils\Log;
use Exception;
use Laminas\Mail\Exception\ExceptionInterface;
@@ -53,6 +55,8 @@ class Service
/**
* @param string $id Account ID.
* @throws Error
* @throws ImapError
* @throws NoImap
*/
public function fetch(string $id): void
{
@@ -64,6 +68,7 @@ class Service
/**
* @return string[]
* @throws Error
* @throws ImapError
*/
public function getFolderList(Params $params): array
{
@@ -103,7 +108,7 @@ class Service
'message' => $e->getMessage(),
]);
$message = $e instanceof ExceptionInterface ?
$message = $e instanceof ExceptionInterface || $e instanceof ImapError ?
$e->getMessage() : '';
throw new ErrorSilent($message);
@@ -126,6 +131,8 @@ class Service
/**
* @param string $id Account ID.
* @throws Error
* @throws ImapError
* @throws NoImap
*/
public function storeSentMessage(string $id, Message $message): void
{

View File

@@ -33,11 +33,16 @@ use Espo\Core\Mail\Account\Storage\Params;
use Espo\Core\Mail\Account\Account;
use Espo\Core\Mail\Account\StorageFactory as StorageFactoryInterface;
use Espo\Core\Mail\Account\Storage\LaminasStorage;
use Espo\Core\Mail\Exceptions\ImapError;
use Espo\Core\Mail\Exceptions\NoImap;
use Espo\Core\Mail\Mail\Storage\Imap;
use Espo\Core\Utils\Log;
use Espo\Core\InjectableFactory;
use Laminas\Mail\Storage\Exception\RuntimeException;
use Laminas\Mail\Storage\Exception\InvalidArgumentException;
use Laminas\Mail\Protocol\Exception\RuntimeException as ProtocolRuntimeException;
use Throwable;
class StorageFactory implements StorageFactoryInterface
@@ -47,9 +52,6 @@ class StorageFactory implements StorageFactoryInterface
private InjectableFactory $injectableFactory
) {}
/**
* @throws NoImap
*/
public function create(Account $account): LaminasStorage
{
$imapParams = $account->getImapParams();
@@ -122,6 +124,13 @@ class StorageFactory implements StorageFactoryInterface
}
}
return new LaminasStorage(new Imap($imapParams));
try {
$storage = new Imap($imapParams);
}
catch (RuntimeException|InvalidArgumentException|ProtocolRuntimeException $e) {
throw new ImapError($e->getMessage(), 0, $e);
}
return new LaminasStorage($storage);
}
}

View File

@@ -30,6 +30,8 @@
namespace Espo\Core\Mail\Account\PersonalAccount;
use Espo\Core\Exceptions\ErrorSilent;
use Espo\Core\Mail\Exceptions\ImapError;
use Espo\Core\Mail\Exceptions\NoImap;
use Espo\Core\Utils\Log;
use Exception;
use Laminas\Mail\Exception\ExceptionInterface;
@@ -56,6 +58,9 @@ class Service
/**
* @param string $id Account ID.
* @throws Error
* @throws NoImap
* @throws ImapError
*
*/
public function fetch(string $id): void
{
@@ -68,6 +73,7 @@ class Service
* @return string[]
* @throws Forbidden
* @throws Error
* @throws ImapError
*/
public function getFolderList(Params $params): array
{
@@ -162,6 +168,8 @@ class Service
/**
* @param string $id Account ID.
* @throws Error
* @throws ImapError
* @throws NoImap
*/
public function storeSentMessage(string $id, Message $message): void
{

View File

@@ -32,6 +32,7 @@ namespace Espo\Core\Mail\Account\PersonalAccount;
use Espo\Core\Mail\Account\Storage\Params;
use Espo\Core\Mail\Account\StorageFactory as StorageFactoryInterface;
use Espo\Core\Mail\Account\Account;
use Espo\Core\Mail\Exceptions\ImapError;
use Espo\Core\Mail\Exceptions\NoImap;
use Espo\Core\Mail\Mail\Storage\Imap;
use Espo\Core\Mail\Account\Storage\LaminasStorage;
@@ -41,6 +42,9 @@ use Espo\Entities\UserData;
use Espo\Repositories\UserData as UserDataRepository;
use Espo\ORM\EntityManager;
use Laminas\Mail\Protocol\Exception\RuntimeException as ProtocolRuntimeException;
use Laminas\Mail\Storage\Exception\InvalidArgumentException;
use Laminas\Mail\Storage\Exception\RuntimeException;
use LogicException;
use Throwable;
@@ -52,9 +56,6 @@ class StorageFactory implements StorageFactoryInterface
private EntityManager $entityManager
) {}
/**
* @throws NoImap
*/
public function create(Account $account): LaminasStorage
{
$userLink = $account->getUser();
@@ -172,9 +173,14 @@ class StorageFactory implements StorageFactoryInterface
}
}
return new LaminasStorage(
new Imap($imapParams)
);
try {
$storage = new Imap($imapParams);
}
catch (RuntimeException|InvalidArgumentException|ProtocolRuntimeException $e) {
throw new ImapError($e->getMessage(), 0, $e);
}
return new LaminasStorage($storage);
}
private function getUserDataRepository(): UserDataRepository

View File

@@ -30,10 +30,23 @@
namespace Espo\Core\Mail\Account;
use Espo\Core\Mail\Account\Storage\Params;
use Espo\Core\Mail\Exceptions\ImapError;
use Espo\Core\Mail\Exceptions\NoImap;
interface StorageFactory
{
/**
* Create an account.
*
* @throws NoImap
* @throws ImapError
*/
public function create(Account $account): Storage;
/**
* Create an account with parameters.
*
* @throws ImapError
*/
public function createWithParams(Params $params): Storage;
}

View File

@@ -0,0 +1,32 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2024 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Mail\Exceptions;
class ImapError extends \Exception {}