Laravel provides App::error()
for showing error pages when certain exceptions are thrown.
http://laravel.com/docs/errors#handling-errors
Likewise you may find try catch is a better method rather than 'listening' for all exceptions thrown. http://php.net/manual/en/language.exceptions.php
Thanks for quick response.
I tried to use this in my global.php
App::error(function(Exception $exception, $code)
{
Log::error($exception);
});
App::error(function(QueryException $exception)
{
echo("error");
});
but nothing show up, apache give me this fatal error
PHP Fatal error: Uncaught exception 'ReflectionException' with message 'Class QueryException does not exist' in /var/www/..../bootstrap/compiled.php:9246\nStack trace
You need to define the namespace of the QueryException, because your QueryException class is not in the same folder as the compiled file.
So maybe you create a new folder called Exceptions in your laravel folder and in the exceptions folder you create the QueryException class.
Catch a \PDOException.
App::error(function (\PDOException $e, $code) {
$message = explode(' ', $e->getMessage());
$dbCode = rtrim($message[1], ']');
$dbCode = trim($dbCode, '[');
// codes specific to MySQL
switch ($dbCode)
{
case 1049:
$userMessage = 'Unknown database - probably config error:';
break;
case 2002:
$userMessage = 'DATABASE IS DOWN:';
break;
default:
$userMessage = 'Untrapped Error:';
break;
}
$userMessage = $userMessage . '<br>' . $e->getMessage();
});
This works for me when the database exists and other failures occur (e.g. table does not exist):
App::error(function (\Illuminate\Database\QueryException $e) {
// Logic here
});
And this works for me when the database name is wrong or does not exist:
App::error(function (PDOException $e) {
// Logic here
});
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community