0
votes

I'm working on a commercial application and I've created Blade files for several kinds of HTTP errors. They are placed in /resources/views/errors and are working fine for authorization (503.blade.php), page not found (404.blade.php), and so on.

I've created files for 400, 403, 404, 500 and 503, until now. The issue is when a QueryException is thrown. In this case, "Whoops, looks like something went wrong." appears.

For example, considering that name cannot be null, when I do something like this, Laravel throws a QueryException:

User::create([
          'name' => null,
          'email' => '[email protected]'
]);

The exception would be:

QueryException in Connection.php line 651: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (SQL: insert into users (nome, email, updated_at, created_at) values (, [email protected], 2018-02-09 12:10:50, 2018-02-09 12:10:50))

I don't want "Whoops, looks like something went wrong." to appear to the end user, I want to show a custom page. What kind of error file do I need to create to achieve this behavior?

1
Just set the debug mode to false in env for production so users will not see this.The Alpha
To @TheAlpha, I have already done that. I don't want to show "Whoops, looks like something went wrong.", I want to show a custom page. I've edited the question to explain this better.El M
remove Whoops thenMahdi Younesi

1 Answers

3
votes

Try this in your controller:

try {
    User::create([
          'name' => null,
          'email' => '[email protected]'
    ]);
} catch ( \Illuminate\Database\QueryException $e) {
    // show custom view
    //Or
    dump($e->errorInfo);
}

To catch all Query Exceptions:

You need to customize the render() method of App\Exceptions\Handler, as stated in the Docs.

public function render($request, Exception $e)
{
    if ( $e instanceof \Illuminate\Database\QueryException ) {
        // show custom view
        //Or
        dump($e->errorInfo);
    } 
   return parent::render($request, $exception);
}