Skip to content

Commit 0dee192

Browse files
authored
Merge pull request #23 from homiedopie/feature/PHP-4
PHP-4 - Add support for error type to be from the error message
2 parents e33b55f + 301cee4 commit 0dee192

4 files changed

Lines changed: 85 additions & 1 deletion

File tree

src/Stackify/Log/Entities/ErrorWrapper.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Stackify\Log\Entities;
44

5+
use Stackify\Log\Transport\Config\AbstractConfig;
6+
use Stackify\Log\Transport\Config\Agent;
7+
58
class ErrorWrapper
69
{
710

@@ -10,6 +13,12 @@ class ErrorWrapper
1013
private $code;
1114
private $trace;
1215
private $sourceMethod;
16+
/**
17+
* Agent config
18+
*
19+
* @var AbstractConfig
20+
*/
21+
private $_config;
1322

1423
const TYPE_STRING_EXCEPTION = 'StringException';
1524
const TRACE_UNKNOWN_ITEM = '{unknown}';
@@ -19,8 +28,10 @@ class ErrorWrapper
1928
*/
2029
private $innerError;
2130

22-
public function __construct($object, $nestingLevel = 1)
31+
public function __construct($object, $nestingLevel = 1, $config = null)
2332
{
33+
$this->_config = !empty($config) ? $config: Agent::getInstance();
34+
2435
if ($object instanceof \Exception) {
2536
$this->message = $object->getMessage();
2637
$this->type = get_class($object);
@@ -50,6 +61,13 @@ public function __construct($object, $nestingLevel = 1)
5061
$this->setTrace(debug_backtrace());
5162
$this->innerError = null;
5263
}
64+
65+
if ($this->_config) {
66+
$classBlacklist = $this->_config->getCaptureExceptionClassBlacklist();
67+
if (!empty($classBlacklist) && isset($classBlacklist[$this->type])) {
68+
$this->type = $this->message;
69+
}
70+
}
5371
}
5472

5573
public function getMessage()

src/Stackify/Log/Transport/Config/AbstractConfig.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ abstract class AbstractConfig
134134
* @var boolean
135135
*/
136136
protected $Debug;
137+
/**
138+
* Exception Class Blacklist
139+
*
140+
* @var array
141+
*/
142+
protected $CaptureExceptionClassBlacklist;
137143

138144

139145
/**
@@ -170,6 +176,7 @@ public function __construct()
170176
$ds = DIRECTORY_SEPARATOR;
171177
$this->DebugLogPath = realpath(dirname(__FILE__) . "$ds..$ds..") . $ds . 'debug/log.log';
172178
$this->Debug = false;
179+
$this->CaptureExceptionClassBlacklist = null;
173180
}
174181

175182
/**
@@ -408,6 +415,17 @@ public function setDebug($enable = null)
408415
{
409416
$this->Debug = $this->getBoolean($enable, 'Debug');
410417
}
418+
/**
419+
* Set capture Exception Class blacklist for ErrorType
420+
*
421+
* @param array $rawConfig From config
422+
*
423+
* @return void
424+
*/
425+
public function setCaptureExceptionClassBlacklist($rawConfig = null)
426+
{
427+
$this->CaptureExceptionClassBlacklist = $this->parseStringToArray($rawConfig, 'CaptureExceptionClassBlacklist');
428+
}
411429

412430

413431
/**
@@ -599,6 +617,15 @@ public function getDebug()
599617
{
600618
return $this->Debug;
601619
}
620+
/**
621+
* Get capture exception class for error type blacklist
622+
*
623+
* @return mixed
624+
*/
625+
public function getCaptureExceptionClassBlacklist()
626+
{
627+
return $this->CaptureExceptionClassBlacklist;
628+
}
602629

603630
/**
604631
* Log error message
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Stackify\Tests\Log\Entities;
4+
5+
use Stackify\Log\Entities\ErrorWrapper;
6+
use Stackify\Log\Transport\Config\Agent;
7+
use Stackify\Tests\Log\Entities\Fixtures\TestException;
8+
9+
class ErrorWrapperTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function testCaptureExceptionBlacklistWithCustomException()
12+
{
13+
$agentConfig = new Agent();
14+
$agentConfig->setCaptureExceptionClassBlacklist([
15+
TestException::class
16+
]);
17+
18+
$message = "Test exception random";
19+
$exception = new TestException($message);
20+
$errorWrapperObject = new ErrorWrapper($exception, 1, $agentConfig);
21+
22+
$this->assertSame($message, $errorWrapperObject->getType());
23+
}
24+
25+
public function testEmptyCaptureExceptionBlacklistWithCustomException()
26+
{
27+
$message = "Test exception random";
28+
$exception = new TestException($message);
29+
$errorWrapperObject = new ErrorWrapper($exception);
30+
31+
$this->assertSame(TestException::class, $errorWrapperObject->getType());
32+
}
33+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace Stackify\Tests\Log\Entities\Fixtures;
4+
5+
class TestException extends \Exception {
6+
}

0 commit comments

Comments
 (0)