Support the ongoing development of Laravel.io →
posted 10 years ago
Database
Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

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
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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();
});
Last updated 2 years ago.
0

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
});
Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

wpg-app wpg-app Joined 8 Sep 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.