Yii2 - error handler can not catches database exceptions - php

I'm working on Yii2 advanced application project and I have a problem with yii2 built-in Error Handler. Here is description of my problem:
I have set errorAction in my backend configuration in this way:
'errorHandler' => [
'errorAction' => 'site/error',
],
Now, the method actionError() in the SiteController catches every application exceptions, user defined exceptions and also fatal errors, except database related exception such as yii\db\IntegrityException. I don't know why, but when such an exception occurs, actionError() does not do anything! I don't want to catch exception manually with try-catch block and I want the central ErrorHandler catches all exception.
How can I fix this problem? Thank you all specially yii experts for paying attention. Please help me to solve this strange problem.

According to this GitHub issue: yiisoft/yii2/issues/10657 and to the comment made by #Yupik you must change the YII_DEBUG.
If YII_DEBUG is true, you should see proper message about yii\db\IntegrityException in debug mode.
If YII_DEBUG is false(like in a production enviroment) error is redirected to actionError() and see it in a user-friendly mode.

Related

Slim 4 error handling not catching base Exception

For some reason, my Slim 4 application error handler did not catch errors which can be caught by \Exception and I see 502 bad gateway error in the browser , here is my ErrorMiddleware configuration (I'm using PHP-DI to configure it ):
$definitions[ErrorMiddleware::class] = static function(ContainerInterface $container): ErrorMiddleware {
$middleware = new ErrorMiddleware(
$container->get(CallableResolverInterface::class),
$container->get(ResponseFactoryInterface::class),
(bool)$container->get(Config::class)->get('main.debug'), //false or true
true,
true
);
$middleware->setErrorHandler(HttpNotFoundException::class, $container->get(NotFoundHandler::class));
return $middleware;
};
I tried to add handler to handle 500 error like this $middleware->setErrorHandler(HttpInternalServerErrorException::class, $container->get(NotFoundHandler::class) );
but it's not working and I still see 502 bad gateway until I surround all controller action with try/catch(\Exception $e).
Do I need to add some other error handlers?? It's not clear for me how to correctly setup up handling in a slim 4 application.
Updated: I found that slim ErrorMiddleware by default catching slim HttpException which is extending Exception, but why not direclty Exception or even Throwable to gracefully exit application with nice error page
Okay it's my bad, adn everithing is fine with a slim, it's failing in the 'log_error()' fucntion as ErrorMiddlware is configured to log the error, but I did not install and configure Monolog yet, so it write logs to php-fpm logs and nginx alongside throwing 502 error

Setting up Chrome Logger Using Laravel Isn't Working

I'm trying to set up Chrome Logger to use alongside Laravel as detailed in "Easy Laravel 5", however following the instructions throws errors, and I'm new to Laravel (and not very experienced in PHP), so I'm not sure how to resolve them. We are directed to add a piece of code to the bootstrap/app.php file to use the chrome logger in the book.
The following is a screenshot of the error screen:
The following is the site without the code excerpt:
I tried requiring the chrome.php file using require_once() in the app.php file, but the error still persists. Removing the code excerpt produces the default screen.
This is the code excerpt:
if ($app->environment('local'))
{
$app->configureMonologUsing(function($monolog)
{
$monolog->pushHandler(new \Monolog\Handler\ChromePHPHandler());
});
}
I expected to be able to use the chrome logger, but instead receive the aforementioned error screen.
Add new channel in your configuration file (config/logging.php):
'chrome' => [
'driver' => 'monolog',
'handler' => \Monolog\Handler\ChromePHPHandler::class,
'formatter' => \Monolog\Formatter\ChromePHPFormatter::class
]
Sometimes you may wish to log a message to a channel other than your application's default channel. You may use the channel method on the Log facade to retrieve and log to any channel defined in your configuration file:
Log::channel('chrome')->info('Something happened!');
If you would like to create an on-demand logging stack consisting of multiple channels, you may use the stack method:
Log::stack(['single', 'chrome'])->info('Something happened!');
You can set new channel by default in your .env file
LOG_CHANNEL=chrome
or you can set
LOG_CHANNEL=stack
and change 'stack' list of channels (config/logging.php) like this:
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'chrome'],
'ignore_exceptions' => false
]
After you can use log with all your list of channels
Log::info('General information log');
From Laravel 5.6 documentation.
The configureMonologUsing Method
If you were using the configureMonologUsing method to customize the
Monolog instance for your application, you should now create a custom
Log channel. For more information on how to create custom channels,
check out the full logging documentation.
Looks like we need to be on the earlier version for this to work.

Using Laravel Debugbar, how can I disable exception logging?

I'm using the Laravel Debugbar Package to display debug information. I'm noticing that the debug bar is logging errors to the log whenever an exception occurs. I would rather this doesn't happen, since I am running my own custom exception handler which collects information in the format I prefer and I have it logging to the log file as well.
So for example when I mistype a route name, I get the message saved twice to the log file:
[2015-02-11 16:21:02] local.ERROR: Method [fdfdgfd] does not exist.
[2015-02-11 16:21:04] local.ERROR: Debugbar exception: Method
UserController::fdfdgfd() does not exist [] []
How can I disable exception logging with the Debugbar?
I know this is old, but in case anyone still needs an answer. Set
'storage' => [ 'enabled' => false, ]
In laravel-debugbar/config/debugbar.php
To prevent or disable exception logging for Debugbar package.
For completely turn off log.
open edit .env file and add DEBUGBAR_ENABLED variable as false value
DEBUGBAR_ENABLED=false
Just want to disable log files in storage directory.
Publish Debugbar configuration file by following command
php artisan vendor:publish --provider=Barryvdh\Debugbar\ServiceProvider
you will get success message like.
Copied File [\vendor\barryvdh\laravel-debugbar\config\debugbar.php] To [\config\debugbar.php]
Publishing complete.
Open \config\debugbar.php file
'storage' => [
'enabled' => false, /// here change it to false
'path' => storage_path('debugbar'),
],
I have never seen this package, but it seems like it provides decent configuration.
Try to set this value to false: https://github.com/barryvdh/laravel-debugbar/blob/1.8/src/config/config.php#L90

Zend Framework 2 error

After a composer update my application don't run anymore.
The error is:
PHP Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\InvalidArgumentException' with message 'Provided abstract factory must be the class name of an abstract factory or an instance of an AbstractFactoryInterface.' in /path_to_app/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php:302
I googled the error and seems that in may 2013 happened something similar that could be solved just commenting two lines in the module.config.php
Naturally, I've tried that but it doesn't seem to work.
Any ideas?
You have a factory service configuration issue in your module.config.php, like the error says one of your configured factory services is not a "class name of an abstract factory or an instance of an AbstractFactoryInterface.".
For debugging purpose, try to temporarily disable your application registered modules one by one to detect in which module this is configured (if you have no idea), until the error disappear.
Then check the services configs.

ZF Error Handler not working

I'm trying to set up custom error handling, so I setup an ErrorController.php file within the default module, under the controllers directory. However, when I try to go to access an invalid controller, an error is shown that an uncaught exception occurred. This bypasses the ErrorController file though, and when I print the contents of the response object, it does not show any exceptions. Does anyone know what I might be doing wrong here? Cheers!
Ok I fixed the issue, apparently all I needed to do was add the following code: $front->throwExceptions(false);

Categories