diff options
author | Matthias Held <ilovemilk@wusa.io> | 2019-07-06 19:16:54 +0300 |
---|---|---|
committer | Matthias Held <ilovemilk@wusa.io> | 2019-07-06 19:16:54 +0300 |
commit | acabb01ea60c7d53c32657c65b0fa985e0463b43 (patch) | |
tree | 9d6c16bdd67eea348b47cc7039f5a28b5cfcb954 | |
parent | 450d60bee7b167ab47d6d84370574d22cfe0d553 (diff) |
add error handling
-rw-r--r-- | lib/AppInfo/Application.php | 6 | ||||
-rw-r--r-- | lib/Monitor.php | 22 | ||||
-rw-r--r-- | lib/Service/FileOperationService.php | 28 |
3 files changed, 36 insertions, 20 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 9dc3b03..19cf9af 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -62,15 +62,15 @@ class Application extends App $container->registerService('FileOperationService', function ($c) { return new FileOperationService( $c->query('FileOperationMapper'), - $c->query(IConfig::class), - $c->query(ILogger::class), + $c->query('OCP\IConfig'), + $c->query('OCP\ILogger'), $c->query('ServerContainer')->getUserSession()->getUser()->getUID() ); }); $container->registerService('ServiceWatcher', function ($c) { return new ServiceWatcher( - $c->query('IConfig') + $c->query('OCP\IConfig') ); }); diff --git a/lib/Monitor.php b/lib/Monitor.php index 2e4cedb..165da29 100644 --- a/lib/Monitor.php +++ b/lib/Monitor.php @@ -39,6 +39,7 @@ use OCP\IConfig; use OCP\ILogger; use OCP\IRequest; use GuzzleHttp\Exception\ConnectException; +use GuzzleHttp\Exception\ServerException; class Monitor { @@ -241,13 +242,12 @@ class Monitor $serviceUri = $this->config->getAppValue(Application::APP_ID, 'service_uri', 'http://localhost:5000'); try { - $result = RequestTemplate::post($serviceUri . "/file-operation", $entity); - if ($result->getStatusCode() !== 200) { - $this->logger->error("The detection service is not working correctly."); - } + RequestTemplate::post($serviceUri . "/file-operation", $entity); } catch (ConnectException $ex) { //TODO: Notify the use by the Notifier $this->logger->error("No connection to the detection service."); + } catch (ServerException $ex) { + $this->logger->error("The detection service is not working correctly."); } $this->nestingLevel--; @@ -375,13 +375,12 @@ class Monitor $serviceUri = $this->config->getAppValue(Application::APP_ID, 'service_uri', 'http://localhost:5000'); try { - $result = RequestTemplate::post($serviceUri . "/file-operation", $entity); - if ($result->getStatusCode() !== 200) { - $this->logger->error("The detection service is not working correctly."); - } + RequestTemplate::post($serviceUri . "/file-operation", $entity); } catch (ConnectException $ex) { //TODO: Notify the use by the Notifier $this->logger->error("No connection to the detection service."); + } catch (ServerException $ex) { + $this->logger->error("The detection service is not working correctly."); } } @@ -417,13 +416,12 @@ class Monitor $serviceUri = $this->config->getAppValue(Application::APP_ID, 'service_uri', 'http://localhost:5000'); try { - $result = RequestTemplate::post($serviceUri . "/file-operation", $entity); - if ($result->getStatusCode() !== 200) { - $this->logger->error("The detection service is not working correctly."); - } + RequestTemplate::post($serviceUri . "/file-operation", $entity); } catch (ConnectException $ex) { //TODO: Notify the use by the Notifier $this->logger->error("No connection to the detection service."); + } catch (ServerException $ex) { + $this->logger->error("The detection service is not working correctly."); } } } diff --git a/lib/Service/FileOperationService.php b/lib/Service/FileOperationService.php index 629bcc5..c545692 100644 --- a/lib/Service/FileOperationService.php +++ b/lib/Service/FileOperationService.php @@ -20,10 +20,14 @@ namespace OCA\RansomwareDetection\Service; +use OCA\RansomwareDetection\AppInfo\Application; +use OCA\RansomwareDetection\RequestTemplate; use OCA\RansomwareDetection\Db\FileOperationMapper; use OCA\RansomwareDetection\Model\Status; use OCP\IConfig; use OCP\ILogger; +use GuzzleHttp\Exception\ClientException; +use GuzzleHttp\Exception\ServerException; class FileOperationService { @@ -47,8 +51,8 @@ class FileOperationService */ public function __construct( FileOperationMapper $mapper, - $config, - $logger, + IConfig $config, + ILogger $logger, $userId ) { $this->mapper = $mapper; @@ -120,11 +124,25 @@ class FileOperationService if ($fileOperation->getStatus() === Status::PENDING) { try { $serviceUri = $this->config->getAppValue(Application::APP_ID, 'service_uri', 'http://localhost:5000'); - $result = RequestTemplate::get($serviceUri . "/file-operation/" + $fileOperation->getId()); - if ($result->getStatusCode() !== 200) { + try { + RequestTemplate::get($serviceUri . "/file-operation/" . $fileOperation->getId()); + } catch (ClientException $ex) { + if ($ex->getResponse()->getStatusCode() === 404) { + // if the detection service doesn't know the file analyze it again. + try { + RequestTemplate::post($serviceUri . "/file-operation", $fileOperation); + } catch(ClientException $ex) { + // already exists + } catch (ServerException $ex) { + $this->logger->error("The detection service is not working correctly."); + } + } else { + // update local file operation and save it to the database + $fileOperation->setStatus(json_decode($result)['status']); + } + } catch (ServerException $ex) { $this->logger->error("The detection service is not working correctly."); } - $this->logger->error($result); } catch (ConnectException $ex) { //TODO: Notify the use by the Notifier $this->logger->error("No connection to the detection service."); |