Skip to content

Commit

Permalink
Catch exceptions in predict command
Browse files Browse the repository at this point in the history
 Signed-off-by: Victor Lap <[email protected]>
  • Loading branch information
victorlap committed Apr 12, 2024
1 parent e1936c1 commit 0a07172
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lib/Command/Predict.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

namespace OCA\SuspiciousLogin\Command;

use OCA\SuspiciousLogin\Exception\ModelNotFoundException;
use OCA\SuspiciousLogin\Exception\ServiceException;
use OCA\SuspiciousLogin\Service\EstimatorService;
use OCA\SuspiciousLogin\Service\Ipv4Strategy;
use OCA\SuspiciousLogin\Service\IpV6Strategy;
Expand Down Expand Up @@ -70,14 +72,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$uid = $input->getArgument('uid');
$ip = $input->getArgument('ip');
$modelId = $input->getArgument('model');
if (!$this->estimatorService->predict(
$uid,
$ip,
$input->getOption('v6') ? new IpV6Strategy() : new Ipv4Strategy(),
$modelId ? (int)$modelId : null)) {
$output->writeln("WARN: IP $ip is suspicious");
return 1;

try {
if (!$this->estimatorService->predict(
$uid,
$ip,
$input->getOption('v6') ? new IpV6Strategy() : new Ipv4Strategy(),
$modelId ? (int)$modelId : null)) {
$output->writeln("WARN: IP $ip is suspicious");
return 1;
}
} catch (ModelNotFoundException $ex) {
$output->writeln('<error>Could not predict suspiciousness: ' . $ex->getMessage() . '</error>');
return 2;
} catch (ServiceException $ex) {
$output->writeln('<error>Could not predict suspiciousness: ' . $ex->getMessage() . '</error>');
return 3;
}

$output->writeln("OK: IP $ip is not suspicious");
return 0;
}
Expand Down

0 comments on commit 0a07172

Please sign in to comment.