Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 - Catch errors

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?

like image 889
TheNiceGuy Avatar asked Jun 16 '13 10:06

TheNiceGuy


People also ask

How show all errors in laravel?

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.

How do I change to 404 in laravel?

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.

How can I catch exception in PHP?

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 ) { // ... }


3 Answers

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.

like image 157
DerLola Avatar answered Nov 01 '22 03:11

DerLola


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());
});
like image 25
Andreas Avatar answered Nov 01 '22 03:11

Andreas


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.

like image 24
arpit desai Avatar answered Nov 01 '22 03:11

arpit desai