Skip to content

feat: add exception RotationFailed #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
22 changes: 20 additions & 2 deletions src/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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
);
}
}
23 changes: 23 additions & 0 deletions src/Exceptions/RotationFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Cesargb\Log\Exceptions;

use Exception;
use Throwable;

class RotationFailed extends Exception
{
private string $filename;

public function __construct(string $message = '', int $code = 0, ?Throwable $previus = null, ?string $filename = null)
{
parent::__construct($message, $code, $previus);

$this->filename = $filename ?? '';
}

public function getFilename(): ?string
{
return $this->filename;
}
}
2 changes: 2 additions & 0 deletions src/Rotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
16 changes: 9 additions & 7 deletions tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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');

Expand Down