-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use trait to tweak file and line of exceptions (#4118)
- Loading branch information
1 parent
ec44f68
commit 958a564
Showing
14 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
*/ | ||
class CastException extends CriticalError | ||
{ | ||
use DebugTraceableTrait; | ||
|
||
/** | ||
* Error code | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
*/ | ||
class ConfigException extends CriticalError | ||
{ | ||
use DebugTraceableTrait; | ||
|
||
/** | ||
* Error code | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace CodeIgniter\Exceptions; | ||
|
||
use Throwable; | ||
|
||
/** | ||
* This trait provides framework exceptions the ability to pinpoint | ||
* accurately where the exception was raised rather than instantiated. | ||
* | ||
* This is used primarily for factory-instantiated exceptions. | ||
*/ | ||
trait DebugTraceableTrait | ||
{ | ||
/** | ||
* Tweaks the exception's constructor to assign the file/line to where | ||
* it is actually raised rather than were it is instantiated. | ||
* | ||
* @param string $message | ||
* @param integer $code | ||
* @param Throwable|null $previous | ||
*/ | ||
public function __construct(string $message = '', int $code = 0, Throwable $previous = null) | ||
{ | ||
parent::__construct($message, $code, $previous); | ||
|
||
$trace = $this->getTrace()[0]; | ||
|
||
if (isset($trace['class']) && $trace['class'] === static::class) | ||
{ | ||
['line' => $this->line, 'file' => $this->file] = $trace; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace CodeIgniter; | ||
|
||
use CodeIgniter\Exceptions\DebugTraceableTrait; | ||
use CodeIgniter\Exceptions\FrameworkException; | ||
use CodeIgniter\Test\CIUnitTestCase; | ||
|
||
/** | ||
* @covers \CodeIgniter\Exceptions\DebugTraceableTrait | ||
*/ | ||
final class DebugTraceableTraitTest extends CIUnitTestCase | ||
{ | ||
public function testFactoryInstanceReturnsWhereItIsRaised(): void | ||
{ | ||
$e1 = new FrameworkException('I am on line 16'); | ||
$e2 = FrameworkException::forEnabledZlibOutputCompression(); | ||
|
||
$this->assertContainsEquals(DebugTraceableTrait::class, class_uses(FrameworkException::class)); | ||
$this->assertSame(16, $e1->getLine()); | ||
$this->assertSame(__FILE__, $e1->getFile()); | ||
$this->assertSame(17, $e2->getLine()); | ||
$this->assertSame(__FILE__, $e2->getFile()); | ||
} | ||
} |