Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use first exception in exceptionHandler() #7341

Merged

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Mar 7, 2023

Description
Fixes #7339

Now the last Exception is shown, but showing the first Exception helps for debugging.
This PR shows the first Exception.

<?php

namespace App\Controllers;

use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class Tests extends BaseController
{
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);

        $this->xx(); // Call to undefined method App\Controllers\Tests::xx()
    }

    public function __destruct()
    {
        dd($this->info); // Undefined property: App\Controllers\Tests::$info
    }

    public function index()
    {
    }
}

Before:

$ php public/index.php tests


[ErrorException]

Undefined property: App\Controllers\Tests::$info

at APPPATH/Controllers/Tests.php:21

Backtrace:
  1    APPPATH/Controllers/Tests.php:21
       CodeIgniter\Debug\Exceptions()->errorHandler(2, 'Undefined property: App\\Controllers\\Tests::$info', '/Users/kenji/work/codeigniter/official/CodeIgniter4/app/Controllers/Tests.php', 21)

  2    SYSTEMPATH/CodeIgniter.php:490
       App\Controllers\Tests()->__destruct()

  3    SYSTEMPATH/CodeIgniter.php:368
       CodeIgniter\CodeIgniter()->handleRequest(null, Object(Config\Cache), false)

  4    FCPATH/index.php:67
       CodeIgniter\CodeIgniter()->run()

After:

$ php public/index.php tests


[Error]

Call to undefined method App\Controllers\Tests::xx()

at APPPATH/Controllers/Tests.php:16

Backtrace:
  1    SYSTEMPATH/CodeIgniter.php:907
       App\Controllers\Tests()->initController(Object(CodeIgniter\HTTP\CLIRequest), Object(CodeIgniter\HTTP\Response), Object(CodeIgniter\Log\Logger))

  2    SYSTEMPATH/CodeIgniter.php:490
       CodeIgniter\CodeIgniter()->createController()

  3    SYSTEMPATH/CodeIgniter.php:368
       CodeIgniter\CodeIgniter()->handleRequest(null, Object(Config\Cache), false)

  4    FCPATH/index.php:67
       CodeIgniter\CodeIgniter()->run()

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Mar 7, 2023
@paulbalandan
Copy link
Member

Would it better to display all exceptions in the stack? Something like:

[Exception 1]
Text
Backtrace:
...

Caused by:

[Exception 2]
Text
Backtrace:
...

Caused by:
...
...

@kenjis
Copy link
Member Author

kenjis commented Mar 7, 2023

I thought about that, but I think one would be simpler.

By the way, I don't know why the first exception is now hidden in the sample code.
Why is it so?

@paulbalandan
Copy link
Member

Not sure about that. Maybe because PHP still called __destruct before calling the exception handler?

@kenjis
Copy link
Member Author

kenjis commented Mar 13, 2023

Maybe because PHP still called __destruct before calling the exception handler?

It seems like that.
I guess that if a class fails to instantiate, the destructor is called.

@kenjis kenjis merged commit 43cf748 into codeigniter4:develop Mar 18, 2023
@kenjis kenjis deleted the fix-exceptionHandler-show-first-exception branch March 18, 2023 00:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: cli request is confusing
3 participants