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?
How do I show laravel validation errors in blade? You can call any() method on $errors variable to check that there are any validation errors exist in it or not. It returns 1 if any errors exist in $errors variable. After that you can call foreach method on $errors->all() and display message using $error variable.
You need to create blade views for error pages, move to this path resources/views/ inside here create errors folder and within the directory create 404. blade. php file. It will redirect you to the 404 page if you don't find the associated URL.
Because exceptions are objects, they all extend a built-in Exception class (see Throwing Exceptions in PHP), which means that catching every PHP exception thrown is as simple as type-hinting the global exception object, which is indicated by adding a backslash in front: try { // ... } catch ( \Exception $e ) { // ... }
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.
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());
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With