Closed fideloper closed 9 years ago
Came across this whilst looking to a solution to handling exceptions in Codeigniter in a sane way. This seems good. Would like to see this become part of the core.
I don't think they will, however. CodeIgniter's convention is to NOT use exceptions, and so adding this functionality in would go against the grain of their choice to use errors over exceptions.
It makes sense to not add it as that could cause havoc going against their own conventions.
That being said, exceptions > errors in my opinion, as they give the programmer better options for handling application errors.
But hey, there's Laravel for that, I guess.
Ahhh, I see. I've not really up on the overall architecture of codeigniter but am aware of their error handling (well, sorta). I guess a lot of it is a throwback to the pre PHP 5 days. I'm with you on the whole Exceptions being better than CI Errors. As something now native to PHP it would be a shame for CI not to adopt them somehow in future revisions.
Coming from C# I found the implementation of exceptions in PHP's core functions and Codeigniter to be disappointing / non existent.
Thanks for the reply btw!
Actually, the CI_Exception::show_php_error method that is the heart of the registered exception handler in CodeIgniter does show a stack trace as long as the app is configured to show exceptions. As such, I'm not clear on what your suggestion would achieve. Maybe you just need to set your error reporting level and display_errors in index.php to make the exception pages show up.
As for routing caught exceptions to a controller for custom display, that is a good idea. #1818 (if merged) will add overrides for regular errors ( as set with show_error() ) and 404s. I could easily apply that same mechanism to show_php_error (thought about it - haven't done it yet).
Between those two things, I think you'd have what you're looking for with this request.
Ah I see. I've taken a quick look at CI_Exception::show_php_error etc and it makes a little more sense. I guess all I would like to see is a simple method to intercept uncaught exceptions before they make it to the predefined codeigniter error handler.
Being able to define a route for uncaught exceptions sound like a good idea. Would be nice to be able to decide at that point what to do with the error and fire out the relevant view / messages etc.
Two things: First, I slightly misstated the location of the stack trace in my previous message - it's actually handled in the default error_php template, not directly in show_php_error. The effect, of course, is the same - the trace shows in the error page when the template is used.
Second, I added exception_override routing to #1818, such that users would be able to specify a URI-style path to an exception handler controller method. The method would receive severity, message, filepath, and line information (after the filtering that happens in show_php_error), followed by any arguments designated in the override route. As it stands now, your handler would have to add the stack trace to the output just like the template does, but I'm considering adding a method to the Exception class to encapsulate that functionality.
CodeIgniter handles errors, but not uncaught Exceptions.
This will eventually result in a pull request. Files involved are:
system/core/CodeIgniter.php
, ~ line 67system/core/Common.php
~ line 467Improvements from this code snippet that would be nice:
What are your thoughts on this?