Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
try...catch a /Database_Exception not working
  • Hi, Am I doing this correctly? Can seem to catch a db error. It goes to the default fuel php_fatal_error view. <code>
    try { \DB::query($sql)->execute();
    } catch (\Database_Exception $e) { return $e->getMessage();
    }
    </code> I need to trap the db error and return as json. Any help would be great. cheers.
  • Hi,
    it appears you are doing it properly.
    2 questions
    1)Are you trying it with some SQL that will actually error?
    2)Whats the error being thrown? I mocked up a quick test and it caught the exception for me
    <pre>
    <code>
    class Controller_Test extends Controller
    {
    public function action_index()
    {
    try
    {
    $result = \DB::query("select * from ZZZZ;")->execute();
    }
    catch(\Database_exception $e)
    {
    echo $e->getMessage();
    }
    }
    }
    </code>
    </pre>
    **the code displays weird for some reason
  • Hi, Yes the sql is producing an error, because fuel is picking up the error and sending its error page. I am using mssql pdo connection if that helps?
  • Can you attach a screenshot of the error?
    (you can just upload it to somewhere like http://www.imgur.com)
  • Are you sure it's an exception, and not a PHP error? Exceptions can be caught, PHP errors can only be reported...
  • Hi guys, the error is a failure on a unique index. However if do:
    try {
      /DB::query('select * from ZZZ')->execute();
    } catch (/Database_Exception $e) {
      echo $e->getMessage();
    }
    

    I can trap the error and all is good. However when I run this insert qry the error is not getting trapped by the try...catch block.
    Xgt6v.png
  • You mean
    try
    {
        \DB::query('select * from ZZZ')->execute();
    } 
    catch (\Database_Exception $e)
    {
        echo $e->getMessage();
    }
    

    The slash has to be a backslash, it's a namespace separator.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion