Skip to content

Commit aaf589e

Browse files
authored
Merge pull request #7 from cesargb/exception
feat: add exception RotationFailed
2 parents 8a603c8 + af81728 commit aaf589e

File tree

5 files changed

+57
-11
lines changed

5 files changed

+57
-11
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ Or you can define the options in the constructor
3939

4040
```php
4141
use Cesargb\Log\Rotation;
42+
use Cesargb\Log\Exceptions\RotationFailed;
4243

4344
$rotation = new Rotation([
4445
'files' => 1,
4546
'compress' => true,
4647
'min-size' => 10,
47-
'then' => function ($filenameTarget, $filenameRotated) {},
48-
'catch' => function ($error) {},
48+
'then' => function ($filename) {},
49+
'catch' => function (RotationFailed $exception) {},
4950
]);
5051

5152
$rotation->rotate('file.log');

src/ErrorHandler.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace Cesargb\Log;
44

5+
use Cesargb\Log\Exceptions\RotationFailed;
56
use Throwable;
67

78
trait ErrorHandler
89
{
910
private $catchCallable = null;
1011

12+
private ?string $_filename = null;
13+
1114
/**
1215
* Call function if roteted catch any Exception.
1316
*/
@@ -18,14 +21,29 @@ public function catch(callable $callable): self
1821
return $this;
1922
}
2023

24+
protected function setFilename(string $filename): void
25+
{
26+
$this->_filename = $filename;
27+
}
28+
2129
protected function exception(Throwable $exception): self
2230
{
2331
if ($this->catchCallable) {
24-
call_user_func($this->catchCallable, $exception);
32+
call_user_func($this->catchCallable, $this->convertException($exception));
2533
} else {
26-
throw $exception;
34+
throw $this->convertException($exception);
2735
}
2836

2937
return $this;
3038
}
39+
40+
private function convertException(Throwable $exception): RotationFailed
41+
{
42+
return new RotationFailed(
43+
$exception->getMessage(),
44+
$exception->getCode(),
45+
$exception->getPrevious(),
46+
$this->_filename
47+
);
48+
}
3149
}

src/Exceptions/RotationFailed.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Cesargb\Log\Exceptions;
4+
5+
use Exception;
6+
use Throwable;
7+
8+
class RotationFailed extends Exception
9+
{
10+
private string $filename;
11+
12+
public function __construct(string $message = '', int $code = 0, ?Throwable $previus = null, ?string $filename = null)
13+
{
14+
parent::__construct($message, $code, $previus);
15+
16+
$this->filename = $filename ?? '';
17+
}
18+
19+
public function getFilename(): ?string
20+
{
21+
return $this->filename;
22+
}
23+
}

src/Rotation.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ public function then(callable $callable): self
8989
*/
9090
public function rotate(string $filename): bool
9191
{
92+
$this->setFilename($filename);
93+
9294
if (!$this->canRotate($filename)) {
9395
return false;
9496
}

tests/ErrorHandlerTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
namespace Cesargb\Log\Test;
44

5-
use Exception;
5+
use Cesargb\Log\Exceptions\RotationFailed;
66
use Cesargb\Log\Rotation;
7-
use Cesargb\Log\Test\TestCase;
87

98
class ErrorHandlerTest extends TestCase
109
{
11-
public function test_throws_exception()
10+
public function testThrowsException()
1211
{
13-
$this->expectException(Exception::class);
12+
$this->expectException(RotationFailed::class);
1413

1514
$rotation = new Rotation();
1615

@@ -19,13 +18,16 @@ public function test_throws_exception()
1918
$this->assertFalse($result);
2019
}
2120

22-
public function test_catch_exception()
21+
public function testCatchException()
2322
{
2423
$rotation = new Rotation();
2524

2625
$result = $rotation
27-
->catch(function ($error) {
28-
26+
->catch(function (RotationFailed $exception) {
27+
$this->assertEquals(
28+
self::DIR_WORK.'file.log',
29+
$exception->getFilename()
30+
);
2931
})
3032
->rotate(self::DIR_WORK.'file.log');
3133

0 commit comments

Comments
 (0)