Comments (5)
Yep, I'm using a decoupled error handler for all 4 major types, was hoping Slim 5 could address the above - but not a deal breaker, Slim is still amazing.
from slim.
Thank you for your comprehensive feedback. It's clear that you have a deep understanding of Slim 4 and its error-handling mechanisms, and we value your perspective.
First, I wanted to add some clarification about PHP error types to our conversation. As you're likely aware, PHP has multiple types of "errors":
- Traditional PHP Errors: These include E_NOTICE, E_WARNING, E_ERROR, etc.
- Exception-based Errors: These involve try-catch blocks and are part of the Exception hierarchy.
In its current form, Slim's ErrorHandler is designed to catch and handle exception-based errors, but it doesn't natively handle traditional PHP errors. The core framework expects you to handle these kinds of errors separately, and you can use PHP's own register_shutdown_function()
and set_error_handler()
functions or similar mechanisms such as Symfony ErrorHandler.
use Symfony\Component\ErrorHandler\ErrorHandler;
// ...
// Throw an exception when a PHP error occurs
// and handled it with the Slim error handler.
ErrorHandler::register();
Regarding your concern about Slim's default error handling getting in the way, I wanted to point out that you can actually remove Slim's default ErrorHandler by simply not adding it in the first place or removing the line $app->addErrorMiddleware(...);
from your code. This gives you full control to implement your own error-handling strategy without any interference from Slim's built-in mechanisms.
from slim.
Thanks for the reply Odan,
Any type of error (even fatal) can occur inside a try-catch (try-catch throwable does not know the future), i.e. in a controller or middleware, as you said, Slim will not handle fatal errors and other "traditional" errors, and that is exactly the problem, if it cannot handle ALL errors then rather remove error handling, because.
Now you need to do gymnastics to implement supplementary error handlers, (I will handle these, Slim will handle those....) and configure it accordingly or maybe not... swtich addErrorMiddleware
and setDefaultErrorHandler
on/off and get different results (I've been using these methods for a long time).
Wouldn't it be nice to have just one, simple, clean, standard, unified way to handle errors in Slim?
from slim.
You've raised some excellent points. It's clear that you've run into the complexities and limitations of Slim's current error-handling system. (I've had the same issues with it)
Note that that Slim's ErrorMiddleware operates within an HTTP-specific context. It's designed to work seamlessly with HTTP request and response objects, making it well-suited for errors that occur during the request-handling process.
However, "traditional" PHP errors, including fatal errors, can occur even before Slim starts running - perhaps during the bootstrap process or other early initialization steps. These errors don't necessarily have an HTTP context, and therefore using a middleware-based approach for handling them doesn't make much sense.
Your suggestion to have a single, unified system to handle all types of errors is very interesting, but due to the "nature" of PHP itself (and not Slim) this is quite "complex". I think it would make more sense to let the developers decide of and how they want to handle a "Throwable" and old-school Errors.
from slim.
Related Issues (20)
- Slim 4 middleware redirects are not working as expected... HOT 3
- understanding the role of $response parameter in Slim controller methods?
- Error responding with stream HOT 2
- version of `App::VERSION` HOT 1
- `App::VERSION` not updated HOT 1
- ResponseEmitter can break a 206 Response with a pre-seeked stream by asserting empty and rewinding. HOT 1
- Slim 4 - Unable to set Expires headers with Middleware HOT 1
- Slim 5: Ensure that route attributes are always in the Request
- Slim 5: Enable header manipulation in ErrorRenderer/ErrorHandler HOT 6
- Add a section to the routing docs callbacks section mentioning first class callable syntax HOT 15
- middleware precedence is not working HOT 2
- Nginx subdirectory HOT 2
- Bump slim 2 to 3 with BC HOT 2
- withHeader is not working HOT 1
- PHP8 Fibers Discussion HOT 4
- Will Slim 3.x support PHP8.1 or higher? HOT 5
- Routing error with array $args but not everytime HOT 2
- RuntimeException for PSR-17 ResponseFactory implementations HOT 1
- PSR 7 http-message version requirement HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from slim.