Reputation: 3730
It seems like its not possible to catch errors yourself, insted of getting the laravel 4 error output.
For example if i try:
$databaseConfig = Config::get('database.connections.mysql');
$connect = mysql_connect($databaseConfig['host'], $databaseConfig['username'], $databaseConfig['password']);
if (!$connect)
{
return 'error';
}
If a error occurs i won't get the "error", insted laravel shows me the exception (on that orange site).
The same if you go ahead and try a
try {
$pdo = DB::connection('mysql')->getPdo();
}
catch(PDOException $exception) {
return Response::make('Database error! ' . $exception->getCode());
}
Is there any way to do that?
Upvotes: 5
Views: 17876
Reputation: 557
I know my answer is very late. But after referencing from above answers, I hadn't solved the problem.
After looking at this site (very clear explanation here): https://stackify.com/php-try-catch-php-exception-tutorial/
Here is an additional answer:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
Upvotes: 0
Reputation: 8029
Take a look at this page: http://laravel.com/docs/errors
Quick example:
App::error(function(PDOException $e)
{
Log::error($exception);
return Response::make('Database error! ' . $exception->getCode());
});
Upvotes: 2
Reputation: 79
App::error(function(Exception $exception) {
echo '<pre>';
echo 'MESSAGE :: ';
print_r($exception->getMessage());
echo '<br> CODE ::';
print_r($exception->getCode());
echo '<br> FILE NAME ::';
print_r($exception->getFile());
echo '<br> LINE NUMBER ::';
print_r($exception->getLine());
die();// if you want than only
});
put this code in your route file...
you will get ERROR MESSAGE with FILE NAME and ERROR LINE
all most errors will be covered.
Upvotes: 1
Reputation: 3918
The code you provided should work just fine. If I put this in my routes.php, I see the expected error string (without the orange).
Route::get('error', function() {
try
{
$pdo = DB::connection('mysql')->getPdo();
}
catch(PDOException $exception)
{
return Response::make('Database error! ' . $exception->getCode());
}
return 'all fine';
});
What might be happening here, is that your PDOException isn't caught. Try added a backslash to the PDOException so you'll be sure it's the one defined in the root and not in the current namespace.
catch(\PDOException $exception)
Also, try to run the code directly from within the routes.php file and see if it behaves the same.
Upvotes: 16