Skip to content

Commit

Permalink
Added socket failure exception formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
matrunchyk committed Jul 31, 2017
1 parent 43e69db commit 5488d37
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"php": ">=5.4.0",
"xenolope/quahog": "2.*",
"illuminate/support": ">=4.2",
"laravel/framework": ">=4.2",
"illuminate/validation": ">=4.1.21"
},
"require-dev": {
Expand Down
8 changes: 4 additions & 4 deletions src/ClamavValidator/ClamavValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function validateClamav($attribute, $value, $parameters)
try {
$socket = (new Factory())->createClient($clamavSocket);
} catch (\Exception $e) {
throw new Exception('Unable to initiate a virus check.');
throw new ClamavValidatorException($e, $attribute, $this);
}

// Create a new instance of the Client
Expand All @@ -62,14 +62,14 @@ public function validateClamav($attribute, $value, $parameters)
$oldPerms = fileperms($file);
chmod($file, 0666);
clearstatcache(true, $file);

// Scan the file
$result = $quahog->scanFile($file);

// Restore permissions
chmod($file, $oldPerms);
clearstatcache(true, $file);


if (self::CLAMAV_STATUS_ERROR === $result['status']) {
throw new ClamavValidatorException($result['reason']);
Expand Down
34 changes: 32 additions & 2 deletions src/ClamavValidator/ClamavValidatorException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
<?php namespace Sunspikes\ClamavValidator;

use Exception;
use Illuminate\Validation\ValidationException;

class ClamavValidatorException extends Exception
class ClamavValidatorException extends ValidationException
{
protected $exception;

public $attribute;

public function __construct(\Exception $exception, string $attribute, \Illuminate\Contracts\Validation\Validator $validator, $response = null)
{
$this->exception = $exception;
$this->attribute = $attribute;

parent::__construct($validator, $this->makeResponse());
}

public function makeResponse()
{
$message = $this->exception->getMessage();

if (substr_count($message, 'SOCKET_ECONNREFUSED')) {
$message = 'Unable to initiate antivirus check. Please try later.';
}

if (request()->expectsJson()) {
return response()->json([
$this->attribute => $message
], 422);
}

return redirect()->back()->withInput(
request()->input()
);
}
}

0 comments on commit 5488d37

Please sign in to comment.