Skip to content

Commit

Permalink
feat: use ExceptionHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Feb 11, 2023
1 parent e753169 commit d2d65cd
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions system/Debug/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
use CodeIgniter\Exceptions\HasExitCodeInterface;
use CodeIgniter\Exceptions\HTTPExceptionInterface;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Config\Exceptions as ExceptionsConfig;
use Config\Paths;
use Config\Services;
use ErrorException;
use Psr\Log\LogLevel;
use Throwable;
Expand All @@ -36,6 +36,8 @@ class Exceptions
* Nesting level of the output buffering mechanism
*
* @var int
*
* @deprecated No longer used. Moved to BaseExceptionHandler.
*/
public $ob_level;

Expand All @@ -44,6 +46,8 @@ class Exceptions
* cli and html error view directories.
*
* @var string
*
* @deprecated No longer used. Moved to BaseExceptionHandler.
*/
protected $viewPath;

Expand All @@ -57,7 +61,7 @@ class Exceptions
/**
* The request.
*
* @var CLIRequest|IncomingRequest
* @var RequestInterface
*/
protected $request;

Expand All @@ -71,12 +75,14 @@ class Exceptions
private ?Throwable $exceptionCaughtByExceptionHandler = null;

/**
* @param CLIRequest|IncomingRequest $request
* @param RequestInterface $request
*/
public function __construct(ExceptionsConfig $config, $request, ResponseInterface $response)
{
// For backward compatibility
$this->ob_level = ob_get_level();
$this->viewPath = rtrim($config->errorViewPath, '\\/ ') . DIRECTORY_SEPARATOR;

$this->config = $config;
$this->request = $request;
$this->response = $response;
Expand Down Expand Up @@ -110,15 +116,31 @@ public function initialize()
* Catches any uncaught errors and exceptions, including most Fatal errors
* (Yay PHP7!). Will log the error, display it if display_errors is on,
* and fire an event that allows custom actions to be taken at this point.
*
* @codeCoverageIgnore
*/
public function exceptionHandler(Throwable $exception)
{
$this->exceptionCaughtByExceptionHandler = $exception;

[$statusCode, $exitCode] = $this->determineCodes($exception);

$this->request = Services::request();
$this->response = Services::response();

if (method_exists($this->config, 'handler')) {
// Use new ExceptionHandler
$handler = $this->config->handler($statusCode, $exception);
$handler->handle(
$exception,
$this->request,
$this->response,
$statusCode,
$exitCode
);

return;
}

// For backward compatibility
if ($this->config->log === true && ! in_array($statusCode, $this->config->ignoreCodes, true)) {
log_message('critical', "{message}\nin {exFile} on line {exLine}.\n{trace}", [
'message' => $exception->getMessage(),
Expand Down Expand Up @@ -212,6 +234,8 @@ public function shutdownHandler()
* whether an HTTP or CLI request, etc.
*
* @return string The path and filename of the view file to use
*
* @deprecated No longer used. Moved to ExceptionHandler.
*/
protected function determineView(Throwable $exception, string $templatePath): string
{
Expand All @@ -238,6 +262,8 @@ protected function determineView(Throwable $exception, string $templatePath): st

/**
* Given an exception and status code will display the error to the client.
*
* @deprecated No longer used. Moved to BaseExceptionHandler.
*/
protected function render(Throwable $exception, int $statusCode)
{
Expand Down Expand Up @@ -282,6 +308,8 @@ protected function render(Throwable $exception, int $statusCode)

/**
* Gathers the variables that will be made available to the view.
*
* @deprecated No longer used. Moved to BaseExceptionHandler.
*/
protected function collectVars(Throwable $exception, int $statusCode): array
{
Expand All @@ -306,6 +334,8 @@ protected function collectVars(Throwable $exception, int $statusCode): array
* Mask sensitive data in the trace.
*
* @param array|object $trace
*
* @deprecated No longer used. Moved to BaseExceptionHandler.
*/
protected function maskSensitiveData(&$trace, array $keysToMask, string $path = '')
{
Expand Down Expand Up @@ -416,6 +446,8 @@ public static function cleanPath(string $file): string
/**
* Describes memory usage in real-world units. Intended for use
* with memory_get_usage, etc.
*
* @deprecated No longer used. Moved to BaseExceptionHandler.
*/
public static function describeMemory(int $bytes): string
{
Expand All @@ -434,6 +466,8 @@ public static function describeMemory(int $bytes): string
* Creates a syntax-highlighted version of a PHP file.
*
* @return bool|string
*
* @deprecated No longer used. Moved to BaseExceptionHandler.
*/
public static function highlightFile(string $file, int $lineNumber, int $lines = 15)
{
Expand Down

0 comments on commit d2d65cd

Please sign in to comment.