Slim: Slim 4 - How to handle errors by ourselves completely?
How can we handle errors by ourselves completely?
I get this error below:
Uncaught Slim\Exception\HttpNotFoundException: Not found.
If I remove these lines:
// $callableResolver = $app->getCallableResolver();
// $responseFactory = $app->getResponseFactory();
// $errorMiddleware = new ErrorMiddleware($callableResolver, $responseFactory, true, true, true);
// $app->add($errorMiddleware);
Then, when we try to catch to the error through a middleware:
$app->add(function (Request $request, RequestHandler $handler) {
try {
$response = $handler->handle($request);
$existingContent = (string) $response->getBody();
...
} catch (\Exception $error) {
$response = $handler->handle($error->getRequest());
$data = [
"status" => $error->getCode(),
"messsage" => $error->getMessage()
];
$payload = json_encode($data);
$response->getBody()->write($payload);
return $response
->withHeader('Content-Type', 'application/json')
->withStatus($status);
};
});
We get the same error back:
Uncaught Slim\Exception\HttpNotFoundException: Not found.
We are trying to make our custom error in the JSON format, for example:
{"status":404,"message":"page not found"}
Any ideas?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 16 (6 by maintainers)
Another way to handle this is to define a default route after all your other route definitions:
I’m closing this as resolved. The docs are clear. If you dislike the architecture you can always use a different framework. You’re also more than welcome to contribute when those architecture decisions are being made, the PRs and discussions are public.
My personal view on these lines below for handling error in slim 4:
I am not sure if this is a PSR standard or not but it is too ugly to read and to understand what is going on.
I don’t know what these two
$callableResolverand$responseFactoryare doing there. if they are not helping us to understand by reading them, they shouldn’t be there.this is just my opinion.
@adriansuter none of that actually works. giving up…
In my view, this should be made simpler. But it is way too complicated now.
@odan , sorry but your “now draw the rest of the f***king owl” answer is not helpful at all. The issue subject literally says “how” and not “if” this is possible, and nobody here even bothered.
So in any case anybody else stumbles over this issue after hours of searching the web (for something that should be properly documented in the first place), I finally got it working like this in Slim
4.14(PHP8.x):Step 1: Install a PSR-7 Implementation
We need to install a PSR-7 implementation, e.g.
nyholm/psr7withcomposer require nyholm/psr7.Step 2: Create custom Error Handler
Step 3: Use custom Error Handler in Slim App
I get your frustration @skizzo. If its so clear then why is it so hard for anyone to say it. Thanks for posting your solution im sure it will help someone. Feel bad for not posting what I did to resolve this now.
@skizzo Thanks man you are the life saver ❤
@lautiamkok it does work, I think you’re doing something wrong on your end. Where are you adding this middleware? It should be the very last middleware you add, right before that middleware should be the routing middleware, otherwise Slim automatically appends the
RoutingMiddlewareif it hasn’t been appended manually, which means it would go in front of your error handling middleware, which means you wouldn’t catch anything.By calling the
handle()method of the handler in the catch-block again, the handler would throw the exception again. But this time, no one is there to catch this second exception.In Slim 4 you could either define your custom error handler (and pass it to the
ErrorMiddleware) or simply define custom error renderers (based on accepted content-types by your request or on a forced content-type).More information can be found in the docs, currently in development: https://github.com/slimphp/Slim-Website/blob/gh-pages-4.x/docs/v4/middleware/error-handling.md