Skip to content

Commit dd0a1e8

Browse files
committed
catch exceptions
1 parent beef537 commit dd0a1e8

File tree

7 files changed

+143
-9
lines changed

7 files changed

+143
-9
lines changed

.vscode/launch.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Listen for XDebug",
9+
"type": "php",
10+
"request": "launch",
11+
"port": 9000
12+
},
13+
{
14+
"name": "Launch currently open script",
15+
"type": "php",
16+
"request": "launch",
17+
"program": "${file}",
18+
"cwd": "${fileDirname}",
19+
"port": 9000
20+
}
21+
]
22+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ $rotation
2626
->compress() // Optional, compress the file after rotated
2727
->files(30) // Optional, files are rotated 30 times before being removed
2828
->minSize(1024) // Optional, are rotated when they grow bigger than 1024 bytes
29+
->then(function ($filename) {}) // Optional, to get filename rotated
30+
->catch(function ($exception) {}) // Optional, to catch a exception in rotating
2931
->rotate('file.log');
3032
```
3133

src/Compress/Gz.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,28 @@
22

33
namespace Cesargb\Log\Compress;
44

5+
use Exception;
6+
57
class Gz
68
{
79
const EXTENSION_COMPRESS = 'gz';
810

9-
public function handler($file): ?string
11+
public function handler($file): string
1012
{
1113
$fileCompress = $file.'.'.self::EXTENSION_COMPRESS;
1214

1315
$fd = fopen($file, 'r');
1416

1517
if (!$fd) {
16-
return null;
18+
throw new Exception("file {$file} not can read.", 100);
1719
}
1820

1921
$gz = gzopen($fileCompress, 'wb');
2022

2123
if (! $gz) {
2224
fclose($fd);
2325

24-
return null;
26+
throw new Exception("file {$fileCompress} not can open.", 101);
2527
}
2628

2729
while (! feof($fd)) {

src/ErrorHandler.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Cesargb\Log;
4+
5+
use Throwable;
6+
7+
class ErrorHandler
8+
{
9+
private $catchCallable = null;
10+
11+
public function catch(callable $callable): self
12+
{
13+
$this->catchCallable = $callable;
14+
15+
return $this;
16+
}
17+
18+
public function exception(Throwable $exception): self
19+
{
20+
if ($this->catchCallable) {
21+
call_user_func($this->catchCallable, $exception);
22+
} else {
23+
throw $exception;
24+
}
25+
26+
return $this;
27+
}
28+
}

src/Rotation.php

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Cesargb\Log\Compress\Gz;
66
use Cesargb\Log\Processors\RotativeProcessor;
7-
use LogicException;
7+
use Exception;
88

99
class Rotation
1010
{
@@ -16,9 +16,13 @@ class Rotation
1616

1717
private $thenCallback = null;
1818

19+
private $errorHandler = null;
20+
1921
public function __construct()
2022
{
2123
$this->processor = new RotativeProcessor();
24+
25+
$this->errorHandler = new ErrorHandler();
2226
}
2327

2428
/**
@@ -75,6 +79,19 @@ public function then(callable $callable): self
7579
return $this;
7680
}
7781

82+
/**
83+
* Call function if roteted catch any Exception.
84+
*
85+
* @param callable $callable
86+
* @return self
87+
*/
88+
public function catch(callable $callable): self
89+
{
90+
$this->errorHandler->catch($callable);
91+
92+
return $this;
93+
}
94+
7895
/**
7996
* Rotate file
8097
*
@@ -94,7 +111,14 @@ public function rotate(string $file): bool
94111
if ($fileRotated && $this->_compress) {
95112
$gz = new Gz();
96113

97-
$fileRotated = $gz->handler($fileRotated);
114+
try {
115+
$fileRotated = $gz->handler($fileRotated);
116+
} catch (Exception $error) {
117+
$this->errorHandler->exception($error);
118+
119+
$fileRotated = null;
120+
}
121+
98122
}
99123

100124
if ($fileRotated && $this->thenCallback) {
@@ -129,12 +153,15 @@ private function runProcessor(string $originalFile, ?string $fileRotated): ?stri
129153
*
130154
* @param string $file
131155
* @return boolean
132-
* @throws LogicException
133156
*/
134157
private function canRotate(string $file): bool
135158
{
136159
if (! $this->fileIsValid($file)) {
137-
throw new LogicException(sprintf('the file %s not is valid.', $file), 2);
160+
$this->errorHandler->exception(
161+
new Exception(sprintf('the file %s not is valid.', $file), 10)
162+
);
163+
164+
return false;
138165
}
139166

140167
return filesize($file) > ($this->_minSize > 0 ? $this->_minSize : 0);
@@ -177,18 +204,33 @@ private function moveContentToTempFile(string $file): ?string
177204
$fd = fopen($file, 'r+');
178205

179206
if (! $fd) {
207+
$this->errorHandler->exception(
208+
new Exception(sprintf('the file %s not can open.', $file), 20)
209+
);
210+
180211
return null;
181212
}
182213

183214
if (! flock($fd, LOCK_EX)) {
184215
fclose($fd);
185216

217+
$this->errorHandler->exception(
218+
new Exception(sprintf('the file %s not can lock.', $file), 21)
219+
);
220+
186221
return null;
187222
}
188223

189224
if (! copy($file, $fileDestination)) {
190225
fclose($fd);
191226

227+
$this->errorHandler->exception(
228+
new Exception(
229+
sprintf('the file %s not can copy to temp file %s.', $file, $fileDestination),
230+
22
231+
)
232+
);
233+
192234
return null;
193235
}
194236

@@ -197,6 +239,10 @@ private function moveContentToTempFile(string $file): ?string
197239

198240
unlink($fileDestination);
199241

242+
$this->errorHandler->exception(
243+
new Exception(sprintf('the file %s not can truncate.', $file), 23)
244+
);
245+
200246
return null;
201247
}
202248

tests/ErrorHandlerTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Cesargb\Log\Test;
4+
5+
use Exception;
6+
use Cesargb\Log\Rotation;
7+
use Cesargb\Log\Test\TestCase;
8+
9+
class ErrorHandlerTest extends TestCase
10+
{
11+
public function test_throws_exception()
12+
{
13+
$this->expectException(Exception::class);
14+
15+
$rotation = new Rotation();
16+
17+
$result = $rotation->rotate(self::DIR_WORK.'file.log');
18+
19+
$this->assertFalse($result);
20+
}
21+
22+
public function test_catch_exception()
23+
{
24+
$rotation = new Rotation();
25+
26+
$result = $rotation
27+
->catch(function ($error) {
28+
29+
})
30+
->rotate(self::DIR_WORK.'file.log');
31+
32+
$this->assertFalse($result);
33+
}
34+
}

tests/RotationTest.php

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

33
namespace Cesargb\Log\Test;
44

5-
use LogicException;
5+
use Exception;
66
use Cesargb\Log\Rotation;
77
use Cesargb\Log\Test\TestCase;
88

99
class RotationTest extends TestCase
1010
{
1111
public function test_log_rotating_if_file_not_exists()
1212
{
13-
$this->expectException(LogicException::class);
13+
$this->expectException(Exception::class);
1414

1515
$rotation = new Rotation();
1616

0 commit comments

Comments
 (0)