Skip to content

Commit

Permalink
Use trait to tweak file and line of exceptions (#4118)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan authored Jan 22, 2021
1 parent ec44f68 commit 958a564
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 0 deletions.
3 changes: 3 additions & 0 deletions system/CLI/Exceptions/CLIException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@

namespace CodeIgniter\CLI\Exceptions;

use CodeIgniter\Exceptions\DebugTraceableTrait;
use RuntimeException;

/**
* CLIException
*/
class CLIException extends RuntimeException
{
use DebugTraceableTrait;

/**
* Thrown when `$color` specified for `$type` is not within the
* allowed list of colors.
Expand Down
3 changes: 3 additions & 0 deletions system/Cache/Exceptions/CacheException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@

namespace CodeIgniter\Cache\Exceptions;

use CodeIgniter\Exceptions\DebugTraceableTrait;
use RuntimeException;

/**
* CacheException
*/
class CacheException extends RuntimeException implements ExceptionInterface
{
use DebugTraceableTrait;

/**
* Thrown when handler has no permission to write cache.
*
Expand Down
3 changes: 3 additions & 0 deletions system/Database/Exceptions/DataException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

namespace CodeIgniter\Database\Exceptions;

use CodeIgniter\Exceptions\DebugTraceableTrait;
use RuntimeException;

class DataException extends RuntimeException implements ExceptionInterface
{
use DebugTraceableTrait;

/**
* Used by the Model's trigger() method when the callback cannot be found.
*
Expand Down
3 changes: 3 additions & 0 deletions system/Encryption/Exceptions/EncryptionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Encryption\Exceptions;

use CodeIgniter\Exceptions\DebugTraceableTrait;
use CodeIgniter\Exceptions\ExceptionInterface;
use RuntimeException;

Expand All @@ -19,6 +20,8 @@
*/
class EncryptionException extends RuntimeException implements ExceptionInterface
{
use DebugTraceableTrait;

/**
* Thrown when no driver is present in the active encryption session.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Exceptions/CastException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
class CastException extends CriticalError
{
use DebugTraceableTrait;

/**
* Error code
*
Expand Down
2 changes: 2 additions & 0 deletions system/Exceptions/ConfigException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
class ConfigException extends CriticalError
{
use DebugTraceableTrait;

/**
* Error code
*
Expand Down
34 changes: 34 additions & 0 deletions system/Exceptions/DebugTraceableTrait.php
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;
}
}
}
2 changes: 2 additions & 0 deletions system/Exceptions/DownloadException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
class DownloadException extends RuntimeException implements ExceptionInterface
{
use DebugTraceableTrait;

public static function forCannotSetFilePath(string $path)
{
return new static(lang('HTTP.cannotSetFilepath', [$path]));
Expand Down
2 changes: 2 additions & 0 deletions system/Exceptions/FrameworkException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
class FrameworkException extends RuntimeException implements ExceptionInterface
{
use DebugTraceableTrait;

public static function forEnabledZlibOutputCompression()
{
return new static(lang('Core.enabledZlibOutputCompression'));
Expand Down
2 changes: 2 additions & 0 deletions system/Exceptions/PageNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

class PageNotFoundException extends OutOfBoundsException implements ExceptionInterface
{
use DebugTraceableTrait;

/**
* Error code
*
Expand Down
3 changes: 3 additions & 0 deletions system/Files/Exceptions/FileException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@

namespace CodeIgniter\Files\Exceptions;

use CodeIgniter\Exceptions\DebugTraceableTrait;
use CodeIgniter\Exceptions\ExceptionInterface;
use RuntimeException;

class FileException extends RuntimeException implements ExceptionInterface
{
use DebugTraceableTrait;

public static function forUnableToMove(string $from = null, string $to = null, string $error = null)
{
return new static(lang('Files.cannotMove', [$from, $to, $error]));
Expand Down
3 changes: 3 additions & 0 deletions system/Files/Exceptions/FileNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@

namespace CodeIgniter\Files\Exceptions;

use CodeIgniter\Exceptions\DebugTraceableTrait;
use CodeIgniter\Exceptions\ExceptionInterface;
use RuntimeException;

class FileNotFoundException extends RuntimeException implements ExceptionInterface
{
use DebugTraceableTrait;

public static function forFileNotFound(string $path)
{
return new static(lang('Files.fileNotFound', [$path]));
Expand Down
3 changes: 3 additions & 0 deletions system/Format/Exceptions/FormatException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Format\Exceptions;

use CodeIgniter\Exceptions\DebugTraceableTrait;
use CodeIgniter\Exceptions\ExceptionInterface;
use RuntimeException;

Expand All @@ -19,6 +20,8 @@
*/
class FormatException extends RuntimeException implements ExceptionInterface
{
use DebugTraceableTrait;

/**
* Thrown when the instantiated class does not exist.
*
Expand Down
25 changes: 25 additions & 0 deletions tests/system/DebugTraceableTraitTest.php
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());
}
}

0 comments on commit 958a564

Please sign in to comment.