diff --git a/retailcrm/controllers/admin/RetailcrmOrdersController.php b/retailcrm/controllers/admin/RetailcrmOrdersController.php index 019df9a..1913526 100644 --- a/retailcrm/controllers/admin/RetailcrmOrdersController.php +++ b/retailcrm/controllers/admin/RetailcrmOrdersController.php @@ -70,6 +70,11 @@ class RetailcrmOrdersController extends RetailcrmAdminAbstractController 'success' => false, 'errorMsg' => $e->getMessage(), ]; + } catch (Error $e) { + return [ + 'success' => false, + 'errorMsg' => $e->getMessage(), + ]; } } } diff --git a/retailcrm/controllers/admin/RetailcrmOrdersUploadController.php b/retailcrm/controllers/admin/RetailcrmOrdersUploadController.php index 2d1ac87..a63291f 100644 --- a/retailcrm/controllers/admin/RetailcrmOrdersUploadController.php +++ b/retailcrm/controllers/admin/RetailcrmOrdersUploadController.php @@ -85,6 +85,8 @@ class RetailcrmOrdersUploadController extends RetailcrmAdminAbstractController $skippedOrders[] = $id_order; } catch (Exception $e) { $errors[$id_order][] = $e->getMessage(); + } catch (Error $e) { + $errors[$id_order][] = $e->getMessage(); } $isSuccessful = $isSuccessful ? $response : false; @@ -102,6 +104,11 @@ class RetailcrmOrdersUploadController extends RetailcrmAdminAbstractController 'success' => false, 'errorMsg' => $e->getMessage(), ]; + } catch (Error $e) { + return [ + 'success' => false, + 'errorMsg' => $e->getMessage(), + ]; } } } diff --git a/retailcrm/lib/RetailcrmCartUploader.php b/retailcrm/lib/RetailcrmCartUploader.php index 1a4b7d0..141078f 100644 --- a/retailcrm/lib/RetailcrmCartUploader.php +++ b/retailcrm/lib/RetailcrmCartUploader.php @@ -217,22 +217,19 @@ class RetailcrmCartUploader { $shouldBeUploaded = true; + $messageTotal = sprintf('Failure while trying to get cart total (cart id=%d)', $cart->id); + $messageProducts = sprintf('Failure while trying to get cart products (cart id=%d)', $cart->id); + try { $currentCartTotal = $cart->getOrderTotal(false, Cart::ONLY_PRODUCTS); if (0 == $currentCartTotal) { $shouldBeUploaded = false; } - } catch (\Exception $exception) { - RetailcrmLogger::writeCaller( - __METHOD__, - sprintf('Failure while trying to get cart total (cart id=%d)', $cart->id) - ); - RetailcrmLogger::writeCaller(__METHOD__, 'Error message and stacktrace will be printed below'); - RetailcrmLogger::writeCaller(__METHOD__, $exception->getMessage()); - RetailcrmLogger::writeNoCaller($exception->getTraceAsString()); - - return true; + } catch (Exception $exception) { + return RetailcrmLogger::writeException(__METHOD__, $exception, $messageTotal, true); + } catch (Error $exception) { + return RetailcrmLogger::writeException(__METHOD__, $exception, $messageTotal, true); } try { @@ -240,16 +237,10 @@ class RetailcrmCartUploader if (0 == count($cart->getProducts(true)) || !$shouldBeUploaded) { return true; } - } catch (\Exception $exception) { - RetailcrmLogger::writeCaller( - __METHOD__, - sprintf('Failure while trying to get cart products (cart id=%d)', $cart->id) - ); - RetailcrmLogger::writeCaller(__METHOD__, 'Error message and stacktrace will be printed below'); - RetailcrmLogger::writeCaller(__METHOD__, $exception->getMessage()); - RetailcrmLogger::writeNoCaller($exception->getTraceAsString()); - - return true; + } catch (Exception $exception) { + return RetailcrmLogger::writeException(__METHOD__, $exception, $messageProducts, true); + } catch (Error $exception) { + return RetailcrmLogger::writeException(__METHOD__, $exception, $messageProducts, true); } return false; @@ -275,12 +266,10 @@ class RetailcrmCartUploader static::$paymentTypes[0], static::$syncStatus ); - } catch (\Exception $exception) { - RetailcrmLogger::writeCaller( - 'abandonedCarts', - $exception->getMessage() . PHP_EOL . $exception->getTraceAsString() - ); - RetailcrmLogger::writeNoCaller($exception->getTraceAsString()); + } catch (Exception $exception) { + RetailcrmLogger::writeException(__METHOD__, $exception, null, true); + } catch (Error $exception) { + RetailcrmLogger::writeException(__METHOD__, $exception, null, true); } return $order; diff --git a/retailcrm/lib/RetailcrmCli.php b/retailcrm/lib/RetailcrmCli.php index 498aa1e..f2531aa 100644 --- a/retailcrm/lib/RetailcrmCli.php +++ b/retailcrm/lib/RetailcrmCli.php @@ -172,14 +172,10 @@ class RetailcrmCli $jobName, $result ? 'true' : 'false' )); - } catch (\Exception $exception) { - if ($exception instanceof RetailcrmJobManagerException && $exception->getPrevious() instanceof \Exception) { - $this->printStack($exception->getPrevious()); - } else { - $this->printStack($exception); - } - - self::clearCurrentJob($jobName); + } catch (Exception $exception) { + $this->handleException($jobName, $exception); + } catch (Error $exception) { + $this->handleException($jobName, $exception); } if (isset($result) && $result) { @@ -190,7 +186,7 @@ class RetailcrmCli /** * Prints error details * - * @param \Exception $exception + * @param Exception|Error $exception * @param string $header */ private function printStack($exception, $header = 'Error while executing a job: ') @@ -361,7 +357,9 @@ class RetailcrmCli } else { RetailcrmLogger::output('Job manager internal state was NOT cleared.'); } - } catch (\Exception $exception) { + } catch (Exception $exception) { + $this->printStack($exception); + } catch (Error $exception) { $this->printStack($exception); } } @@ -452,4 +450,20 @@ class RetailcrmCli 'RetailcrmClearLogsEvent', ]; } + + private function handleException($jobName, $exception) + { + if ($exception instanceof RetailcrmJobManagerException + && ( + $exception->getPrevious() instanceof Exception + || $exception->getPrevious() instanceof Error + ) + ) { + $this->printStack($exception->getPrevious()); + } else { + $this->printStack($exception); + } + + self::clearCurrentJob($jobName); + } } diff --git a/retailcrm/lib/RetailcrmExport.php b/retailcrm/lib/RetailcrmExport.php index 155271c..ff77d8e 100644 --- a/retailcrm/lib/RetailcrmExport.php +++ b/retailcrm/lib/RetailcrmExport.php @@ -175,10 +175,10 @@ class RetailcrmExport try { $orders[] = $orderBuilder->buildOrderWithPreparedCustomer(); - } catch (\InvalidArgumentException $exception) { - RetailcrmLogger::writeCaller('export', sprintf('Error while building %s: %s', $record['id_order'], $exception->getMessage())); - RetailcrmLogger::writeNoCaller($exception->getTraceAsString()); - RetailcrmLogger::output($exception->getMessage()); + } catch (Exception $exception) { + self::handleError($record['id_order'], $exception); + } catch (Error $exception) { + self::handleError($record['id_order'], $exception); } time_nanosleep(0, 250000000); @@ -346,10 +346,10 @@ class RetailcrmExport try { $customers[] = RetailcrmOrderBuilder::buildCrmCustomer($cmsCustomer, $address); - } catch (\Exception $exception) { - RetailcrmLogger::writeCaller('export', sprintf('Error while building %s: %s', $customerId, $exception->getMessage())); - RetailcrmLogger::writeNoCaller($exception->getTraceAsString()); - RetailcrmLogger::output($exception->getMessage()); + } catch (Exception $exception) { + self::handleError($customerId, $exception); + } catch (Error $exception) { + self::handleError($customerId, $exception); } if (50 == count($customers)) { @@ -452,4 +452,12 @@ class RetailcrmExport return true; } + + private static function handleError($entityId, $exception) + { + RetailcrmLogger::writeException('export', $exception, sprintf( + 'Error while building %s: %s', $entityId, $exception->getMessage() + ), true); + RetailcrmLogger::output($exception->getMessage()); + } } diff --git a/retailcrm/lib/RetailcrmJobManager.php b/retailcrm/lib/RetailcrmJobManager.php index 8df18fe..7ab89e4 100644 --- a/retailcrm/lib/RetailcrmJobManager.php +++ b/retailcrm/lib/RetailcrmJobManager.php @@ -119,6 +119,16 @@ class RetailcrmJobManager $jobs ); + return; + } catch (Error $exception) { + static::handleError( + $exception->getFile(), + $exception->getMessage(), + $exception->getTraceAsString(), + '', + $jobs + ); + return; } @@ -203,9 +213,9 @@ class RetailcrmJobManager break; } - } catch (\Exception $e) { + } catch (Exception $e) { $exception = $e; - } catch (\Throwable $e) { + } catch (Error $e) { $exception = $e; } @@ -247,6 +257,14 @@ class RetailcrmJobManager '', $jobs ); + } catch (Error $exception) { + static::handleError( + $exception->getFile(), + $exception->getMessage(), + $exception->getTraceAsString(), + '', + $jobs + ); } static::unlock(); @@ -260,7 +278,7 @@ class RetailcrmJobManager * @return bool * * @throws Exception - * @throws Throwable + * @throws Error */ public static function execManualJob($jobName) { @@ -276,25 +294,13 @@ class RetailcrmJobManager } return $result; - } catch (\Exception $exception) { - if ($exception instanceof RetailcrmJobManagerException - && $exception->getPrevious() instanceof \Exception - ) { - $exception = $exception->getPrevious(); - } - - RetailcrmLogger::printException($exception, '', false); - self::updateLastRunDetail($jobName, [ - 'success' => false, - 'lastRun' => new \DateTimeImmutable('now'), - 'error' => [ - 'message' => $exception->getMessage(), - 'trace' => $exception->getTraceAsString(), - ], - ]); - - throw $exception; + } catch (Exception $exception) { + self::handleManualRunError($jobName, $exception); + } catch (Error $exception) { + self::handleManualRunError($jobName, $exception); } + + return false; } /** @@ -455,7 +461,9 @@ class RetailcrmJobManager return static::execHere($jobName, $cliMode, $force, $shopId); } catch (\RetailcrmJobManagerException $exception) { throw $exception; - } catch (\Exception $exception) { + } catch (Exception $exception) { + throw new RetailcrmJobManagerException($exception->getMessage(), $job, [], 0, $exception); + } catch (Error $exception) { throw new RetailcrmJobManagerException($exception->getMessage(), $job, [], 0, $exception); } } @@ -600,6 +608,13 @@ class RetailcrmJobManager $exception->getTraceAsString(), $job ); + } catch (Error $exception) { + static::handleError( + $exception->getFile(), + $exception->getMessage(), + $exception->getTraceAsString(), + $job + ); } } @@ -811,4 +826,34 @@ class RetailcrmJobManager return false; } + + /** + * @param $jobName + * @param Exception|Error $exception + * + * @throws Exception|Error + */ + private static function handleManualRunError($jobName, $exception) + { + if ($exception instanceof RetailcrmJobManagerException + && ( + $exception->getPrevious() instanceof Exception + || $exception->getPrevious() instanceof Error + ) + ) { + $exception = $exception->getPrevious(); + } + + RetailcrmLogger::printException($exception, '', false); + self::updateLastRunDetail($jobName, [ + 'success' => false, + 'lastRun' => new \DateTimeImmutable('now'), + 'error' => [ + 'message' => $exception->getMessage(), + 'trace' => $exception->getTraceAsString(), + ], + ]); + + throw $exception; + } } diff --git a/retailcrm/lib/RetailcrmLogger.php b/retailcrm/lib/RetailcrmLogger.php index e6a5269..e0263e1 100755 --- a/retailcrm/lib/RetailcrmLogger.php +++ b/retailcrm/lib/RetailcrmLogger.php @@ -128,8 +128,9 @@ class RetailcrmLogger /** * Output error info to stdout * - * @param $exception + * @param Exception|Error $exception * @param string $header + * @param bool $toOutput */ public static function printException($exception, $header = 'Error while executing a job: ', $toOutput = true) { @@ -327,4 +328,19 @@ class RetailcrmLogger return $reduced; } + + public static function writeException($caller, $exception, $message = null, $withTrace = false) + { + if (null !== $message) { + RetailcrmLogger::writeCaller($caller, $message); + } + + RetailcrmLogger::writeCaller($caller, $exception->getMessage()); + + if ($withTrace) { + RetailcrmLogger::writeNoCaller($exception->getTraceAsString()); + } + + return true; + } } diff --git a/retailcrm/lib/RetailcrmReferences.php b/retailcrm/lib/RetailcrmReferences.php index 94322fb..c6a8d2f 100644 --- a/retailcrm/lib/RetailcrmReferences.php +++ b/retailcrm/lib/RetailcrmReferences.php @@ -409,13 +409,9 @@ class RetailcrmReferences return current($response['sites']); } } catch (Exception $e) { - RetailcrmLogger::writeCaller( - __METHOD__, - sprintf( - 'Error: %s', - $e->getMessage() - ) - ); + RetailcrmLogger::writeException(__METHOD__, $e->getMessage(), null, false); + } catch (Error $e) { + RetailcrmLogger::writeException(__METHOD__, $e->getMessage(), null, false); } return null; diff --git a/retailcrm/lib/RetailcrmTools.php b/retailcrm/lib/RetailcrmTools.php index 507a9a7..f6b5e75 100644 --- a/retailcrm/lib/RetailcrmTools.php +++ b/retailcrm/lib/RetailcrmTools.php @@ -218,7 +218,7 @@ class RetailcrmTools /** * Dumps entity using it's definition mapping. * - * @param \ObjectModel $object + * @param $object * * @return array|string */ @@ -819,8 +819,9 @@ class RetailcrmTools return (null === $result || false === $result) ? $object : $result; } catch (Exception $e) { - RetailcrmLogger::writeCaller(__METHOD__, 'Error in custom filter: ' . $e->getMessage()); - RetailcrmLogger::writeDebug(__METHOD__, $e->getTraceAsString()); + RetailcrmLogger::writeException(__METHOD__, $e, 'Error in custom filter', true); + } catch (Error $e) { + RetailcrmLogger::writeException(__METHOD__, $e, 'Error in custom filter', true); } return $object; diff --git a/retailcrm/lib/api/middleware/RetailcrmExceptionMiddleware.php b/retailcrm/lib/api/middleware/RetailcrmExceptionMiddleware.php index 5a360b4..63d81a9 100644 --- a/retailcrm/lib/api/middleware/RetailcrmExceptionMiddleware.php +++ b/retailcrm/lib/api/middleware/RetailcrmExceptionMiddleware.php @@ -45,16 +45,51 @@ class RetailcrmExceptionMiddleware implements RetailcrmMiddlewareInterface { try { $response = $next($request); - } catch (Exception $e) { - RetailcrmLogger::writeCaller($request->getMethod(), $e->getMessage()); - RetailcrmLogger::writeNoCaller($e->getTraceAsString()); - $response = new RetailcrmApiResponse(500, json_encode([ - 'success' => false, - 'errorMsg' => sprintf('Internal error: %s', $e->getMessage()), - ])); + $this->checkResponseType($response); + } catch (Exception $e) { + $response = $this->getInvalidResponse($request, $e); + } catch (Error $e) { + $response = $this->getInvalidResponse($request, $e); } return $response; } + + /** + * @throws Exception + */ + private function checkResponseType($response) + { + if (!($response instanceof RetailcrmApiResponse)) { + throw new Exception( + sprintf( + 'Expected instance of `%s`, but `%s` given', + RetailcrmApiResponse::class, + (is_object($response) ? get_class($response) : gettype($response)) + ) + ); + } + } + + /** + * @param RetailcrmApiRequest $request + * @param Exception|Error $exception + * + * @return RetailcrmApiResponse + */ + private function getInvalidResponse(RetailcrmApiRequest $request, $exception) + { + $errorMsg = sprintf('Internal error: %s', $exception->getMessage()); + + RetailcrmLogger::writeCaller($request->getMethod(), $errorMsg); + RetailcrmLogger::writeNoCaller($exception->getTraceAsString()); + + return new RetailcrmApiResponse( + 500, json_encode([ + 'success' => false, + 'errorMsg' => $errorMsg, + ]) + ); + } } diff --git a/retailcrm/lib/api/middleware/RetailcrmExportOrdersMiddleware.php b/retailcrm/lib/api/middleware/RetailcrmExportOrdersMiddleware.php index 49b412e..f730678 100644 --- a/retailcrm/lib/api/middleware/RetailcrmExportOrdersMiddleware.php +++ b/retailcrm/lib/api/middleware/RetailcrmExportOrdersMiddleware.php @@ -41,7 +41,7 @@ class RetailcrmExportOrdersMiddleware implements RetailcrmMiddlewareInterface /** * {@inheritDoc} * - * @throws Exception + * @throws Exception|Error */ public function __invoke(RetailcrmApiRequest $request, callable $next = null) { @@ -83,11 +83,9 @@ class RetailcrmExportOrdersMiddleware implements RetailcrmMiddlewareInterface return $response; } catch (Exception $e) { - if (isset($order['externalId'])) { - RetailcrmExportOrdersHelper::updateExportState($order['externalId'], null, [$e->getMessage()]); - } - - throw $e; + $this->handleError($order, $e); + } catch (Error $e) { + $this->handleError($order, $e); } } @@ -97,7 +95,7 @@ class RetailcrmExportOrdersMiddleware implements RetailcrmMiddlewareInterface * * @return RetailcrmApiResponse * - * @throws Exception + * @throws Exception|Error */ private function handleOrdersUpload(RetailcrmApiRequest $request, callable $next) { @@ -112,6 +110,12 @@ class RetailcrmExportOrdersMiddleware implements RetailcrmMiddlewareInterface RetailcrmExportOrdersHelper::updateExportState($id_order, null, [$e->getMessage()]); } + throw $e; + } catch (Error $e) { + foreach ($requestedOrders as $id_order) { + RetailcrmExportOrdersHelper::updateExportState($id_order, null, [$e->getMessage()]); + } + throw $e; } @@ -169,4 +173,18 @@ class RetailcrmExportOrdersMiddleware implements RetailcrmMiddlewareInterface return $orders; } + + /** + * @throws Exception|Error + */ + private function handleError($order, $e) + { + if (isset($order['externalId'])) { + RetailcrmExportOrdersHelper::updateExportState( + $order['externalId'], null, [$e->getMessage()] + ); + } + + throw $e; + } } diff --git a/retailcrm/retailcrm.php b/retailcrm/retailcrm.php index 9cc7c36..826529d 100644 --- a/retailcrm/retailcrm.php +++ b/retailcrm/retailcrm.php @@ -454,6 +454,9 @@ class RetailCRM extends Module } catch (Exception $e) { $this->displayError($e->getMessage()); RetailcrmLogger::writeCaller(__METHOD__, $e->getTraceAsString()); + } catch (Error $e) { + $this->displayError($e->getMessage()); + RetailcrmLogger::writeCaller(__METHOD__, $e->getTraceAsString()); } $isSuccessful = $isSuccessful ? $response : false; @@ -512,6 +515,13 @@ class RetailCRM extends Module $this->l('was completed with errors'), $e->getMessage() )); + } catch (Error $e) { + return $this->displayError(sprintf( + '%s %s: %s', + $this->l($jobNameFront), + $this->l('was completed with errors'), + $e->getMessage() + )); } } @@ -643,7 +653,9 @@ class RetailCRM extends Module } return RetailcrmJsonResponse::successfullResponse(); - } catch (\Exception $exception) { + } catch (Exception $exception) { + return RetailcrmJsonResponse::invalidResponse($exception->getMessage()); + } catch (Error $exception) { return RetailcrmJsonResponse::invalidResponse($exception->getMessage()); } } @@ -810,6 +822,9 @@ class RetailCRM extends Module } catch (Exception $e) { RetailcrmLogger::writeCaller(__METHOD__, $e->getMessage()); RetailcrmLogger::writeNoCaller($e->getTraceAsString()); + } catch (Error $e) { + RetailcrmLogger::writeCaller(__METHOD__, $e->getMessage()); + RetailcrmLogger::writeNoCaller($e->getTraceAsString()); } return false; @@ -831,6 +846,9 @@ class RetailCRM extends Module } catch (Exception $e) { RetailcrmLogger::writeCaller(__METHOD__, $e->getMessage()); RetailcrmLogger::writeNoCaller($e->getTraceAsString()); + } catch (Error $e) { + RetailcrmLogger::writeCaller(__METHOD__, $e->getMessage()); + RetailcrmLogger::writeNoCaller($e->getTraceAsString()); } return false; @@ -1434,6 +1452,8 @@ class RetailCRM extends Module * Activity indicator in cache will be rewrited by current state. * * @return array + * + * @throws PrestaShopException */ public static function getCachedCmsModulesList() { @@ -1452,46 +1472,50 @@ class RetailCRM extends Module static::writeModulesCache($serializedModules); return static::$moduleListCache; - } else { - try { - if (is_array(static::$moduleListCache)) { - return static::$moduleListCache; - } - - $modulesList = static::requireModulesCache(); - - if (false === $modulesList) { - Configuration::updateValue(static::MODULE_LIST_CACHE_CHECKSUM, 'not exist'); - - return static::getCachedCmsModulesList(); - } - - static::$moduleListCache = []; - - foreach ($modulesList as $serializedModule) { - $deserialized = json_decode($serializedModule); - - if ($deserialized instanceof stdClass - && property_exists($deserialized, 'name') - && property_exists($deserialized, 'active') - ) { - $deserialized->active = Module::isEnabled($deserialized->name); - static::$moduleListCache[] = $deserialized; - } - } - - static::$moduleListCache = array_filter(static::$moduleListCache); - unset($modulesList); + } + try { + if (is_array(static::$moduleListCache)) { return static::$moduleListCache; - } catch (Exception $exception) { - RetailcrmLogger::writeCaller(__METHOD__, $exception->getMessage()); - RetailcrmLogger::writeNoCaller($exception->getTraceAsString()); - Configuration::updateValue(static::MODULE_LIST_CACHE_CHECKSUM, 'exception'); + } + + $modulesList = static::requireModulesCache(); + + if (false === $modulesList) { + Configuration::updateValue(static::MODULE_LIST_CACHE_CHECKSUM, 'not exist'); return static::getCachedCmsModulesList(); } + + static::$moduleListCache = []; + + foreach ($modulesList as $serializedModule) { + $deserialized = json_decode($serializedModule); + + if ($deserialized instanceof stdClass + && property_exists($deserialized, 'name') + && property_exists($deserialized, 'active') + ) { + $deserialized->active = Module::isEnabled($deserialized->name); + static::$moduleListCache[] = $deserialized; + } + } + + static::$moduleListCache = array_filter(static::$moduleListCache); + unset($modulesList); + + return static::$moduleListCache; + } catch (Exception $exception) { + RetailcrmLogger::writeCaller(__METHOD__, $exception->getMessage()); + RetailcrmLogger::writeNoCaller($exception->getTraceAsString()); + } catch (Error $exception) { + RetailcrmLogger::writeCaller(__METHOD__, $exception->getMessage()); + RetailcrmLogger::writeNoCaller($exception->getTraceAsString()); } + + Configuration::updateValue(static::MODULE_LIST_CACHE_CHECKSUM, 'exception'); + + return static::getCachedCmsModulesList(); } /** diff --git a/tests/helpers/RetailcrmTestCase.php b/tests/helpers/RetailcrmTestCase.php index c611e25..1642a99 100644 --- a/tests/helpers/RetailcrmTestCase.php +++ b/tests/helpers/RetailcrmTestCase.php @@ -42,8 +42,14 @@ if (class_exists('LegacyTests\Unit\ContextMocker')) { abstract class RetailcrmTestCase extends \PHPUnit\Framework\TestCase { + /** + * @var RetailcrmProxy + */ private $apiMock; + /** + * @var PHPUnit_Framework_MockObject_MockObject + */ protected $apiClientMock; protected $contextMock; diff --git a/tests/lib/api/RetailcrmExceptionMiddlewareTest.php b/tests/lib/api/RetailcrmExceptionMiddlewareTest.php index 7a58380..228ece5 100644 --- a/tests/lib/api/RetailcrmExceptionMiddlewareTest.php +++ b/tests/lib/api/RetailcrmExceptionMiddlewareTest.php @@ -45,9 +45,18 @@ class RetailcrmExceptionMiddlewareTest extends RetailcrmTestCase parent::setUp(); $this->api = RetailcrmTools::getApiClient(); + $this->apiMock = $this->getApiMock( + [ + 'ordersGet', + 'ordersEdit', + 'ordersPaymentEdit', + 'ordersCreate', + 'ordersUpload', + ] + ); } - public function getRequests() + public function getRequestsBadParams() { return [ [ @@ -99,13 +108,72 @@ class RetailcrmExceptionMiddlewareTest extends RetailcrmTestCase } /** - * @dataProvider getRequests + * @dataProvider getRequestsBadParams */ - public function testRequest($method, $params, $errorMsg) + public function testRequestBadParams($method, $params, $errorMsg) { /** @var RetailcrmApiResponse $response */ $response = call_user_func_array([$this->api, $method], $params); + $this->checkResponse($response, $errorMsg); + } + + public function getRequestsException() + { + return [ + [ + 'method' => 'ordersGet', + 'params' => [406, 'id'], + ], + [ + 'method' => 'ordersEdit', + 'params' => [['id' => 406], 'id'], + ], + [ + 'method' => 'ordersPaymentEdit', + 'params' => [['id' => 406], 'id'], + ], + [ + 'method' => 'ordersCreate', + 'params' => [['id' => 407]], + ], + [ + 'method' => 'ordersUpload', + 'params' => [[['externalId' => 1]]], + ], + ]; + } + + /** + * @dataProvider getRequestsException + */ + public function testRequestException($method, $params) + { + $errorMsg = 'Test exception ' . md5(json_encode([$method, $params])); + + $this->makeRequestException($method, $params, $errorMsg, function () use ($errorMsg) { + throw new Exception($errorMsg); + }); + + if (class_exists('Error')) { + $this->makeRequestException($method, $params, $errorMsg, function () use ($errorMsg) { + throw new Error($errorMsg); + }); + } + } + + private function makeRequestException($method, $params, $errorMsg, $return) + { + $this->apiClientMock->expects($this->any())->method($method)->willReturnCallback($return); + + /** @var RetailcrmApiResponse $response */ + $response = call_user_func_array([$this->apiMock, $method], $params); + + $this->checkResponse($response, $errorMsg); + } + + private function checkResponse(RetailcrmApiResponse $response, $errorMsg) + { $this->assertInstanceOf(RetailcrmApiResponse::class, $response); $this->assertFalse($response->isSuccessful()); $this->assertStringStartsWith('Internal error: ', $response['errorMsg']);