5

In my controller i need to test custom database connection and if it's bad return an error. The problem is that catch block doesn't work... I get an exception message which is globally defined in app/start/global.php.

try {
    DB::connection('test');
} catch (Exception $e) {
    dd('error');
}
7
  • what happens with this code ? "doesn't work" is too wide... Commented Jul 23, 2015 at 13:14
  • @Random as i said it doesn't work. When connection is bad it should show error message from dd, but it just throws original laravel exception Commented Jul 23, 2015 at 13:18
  • it may have an error_handler. maybe DB::connection throws an error (so no exception), and Laravel catches it to end it its own way... can you see what is the error handler ? Commented Jul 23, 2015 at 13:20
  • @Random "type":"PDOException","message":"SQLSTATE[HY000] [2002] Connection refused", Commented Jul 23, 2015 at 13:26
  • 8
    Do "catch (\Exception $e)" and it should work Commented Jul 23, 2015 at 13:34

1 Answer 1

3

Laravel handles exceptions, no need for try/catch. You can write a custom exception inside \App\Exceptions and customise the contents of App\Exceptions\Handler.php to format the response of any individual exception to meet your needs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.