diff --git a/README.md b/README.md index aed2fea..12c31c0 100644 --- a/README.md +++ b/README.md @@ -39,13 +39,14 @@ Or you can define the options in the constructor ```php use Cesargb\Log\Rotation; +use Cesargb\Log\Exceptions\RotationFailed; $rotation = new Rotation([ 'files' => 1, 'compress' => true, 'min-size' => 10, - 'then' => function ($filenameTarget, $filenameRotated) {}, - 'catch' => function ($error) {}, + 'then' => function ($filename) {}, + 'catch' => function (RotationFailed $exception) {}, ]); $rotation->rotate('file.log'); diff --git a/src/ErrorHandler.php b/src/ErrorHandler.php index 655bb0c..541d28c 100644 --- a/src/ErrorHandler.php +++ b/src/ErrorHandler.php @@ -2,12 +2,15 @@ namespace Cesargb\Log; +use Cesargb\Log\Exceptions\RotationFailed; use Throwable; trait ErrorHandler { private $catchCallable = null; + private ?string $_filename = null; + /** * Call function if roteted catch any Exception. */ @@ -18,14 +21,29 @@ public function catch(callable $callable): self return $this; } + protected function setFilename(string $filename): void + { + $this->_filename = $filename; + } + protected function exception(Throwable $exception): self { if ($this->catchCallable) { - call_user_func($this->catchCallable, $exception); + call_user_func($this->catchCallable, $this->convertException($exception)); } else { - throw $exception; + throw $this->convertException($exception); } return $this; } + + private function convertException(Throwable $exception): RotationFailed + { + return new RotationFailed( + $exception->getMessage(), + $exception->getCode(), + $exception->getPrevious(), + $this->_filename + ); + } } diff --git a/src/Exceptions/RotationFailed.php b/src/Exceptions/RotationFailed.php new file mode 100644 index 0000000..3db5ddb --- /dev/null +++ b/src/Exceptions/RotationFailed.php @@ -0,0 +1,23 @@ +filename = $filename ?? ''; + } + + public function getFilename(): ?string + { + return $this->filename; + } +} diff --git a/src/Rotation.php b/src/Rotation.php index dacfa71..3d94791 100644 --- a/src/Rotation.php +++ b/src/Rotation.php @@ -89,6 +89,8 @@ public function then(callable $callable): self */ public function rotate(string $filename): bool { + $this->setFilename($filename); + if (!$this->canRotate($filename)) { return false; } diff --git a/tests/ErrorHandlerTest.php b/tests/ErrorHandlerTest.php index 552da59..9b754fa 100644 --- a/tests/ErrorHandlerTest.php +++ b/tests/ErrorHandlerTest.php @@ -2,15 +2,14 @@ namespace Cesargb\Log\Test; -use Exception; +use Cesargb\Log\Exceptions\RotationFailed; use Cesargb\Log\Rotation; -use Cesargb\Log\Test\TestCase; class ErrorHandlerTest extends TestCase { - public function test_throws_exception() + public function testThrowsException() { - $this->expectException(Exception::class); + $this->expectException(RotationFailed::class); $rotation = new Rotation(); @@ -19,13 +18,16 @@ public function test_throws_exception() $this->assertFalse($result); } - public function test_catch_exception() + public function testCatchException() { $rotation = new Rotation(); $result = $rotation - ->catch(function ($error) { - + ->catch(function (RotationFailed $exception) { + $this->assertEquals( + self::DIR_WORK.'file.log', + $exception->getFilename() + ); }) ->rotate(self::DIR_WORK.'file.log');