简体   繁体   中英

How can I handle this kind of exceptions (in Doctrine)

Can you tell me how can I handle this kind of exceptions:

Fatal error: Uncaught exception 'Doctrine_Connection_Exception' with message 'PDO Connection Error: SQLSTATE[HY000] [2013] Lost connection to MySQL server at 'reading initial communication packet', system error: 110' in ...

It happens when connection with MySQL is lost during query. I need to handle this exception so I can show 500 error page so the crawlers do not cache page, and to redirect user to appropriate "Try again" page.

PS I have a lot's of code, so I can not go trough all code to put try/catch block. I need something simple and yet effective.

I need something simple and yet effective

try/catch will be your only choice IMO. If you have to many places where you need to add a try/catch clause, you could move this particulat piece of code into a seperate function.

If, say, this line throws the execption

$con->query($your_DQL_query) //something happends here with the results

move it to an something like

function goodQuery($sql) {
  try {
    $con->query($sql);
  } catch (Doctrine_Connection_Exception $dce) {
    //do something
  }
}

You still need to replace all of your "throwing" methods with a call to function goodQuery(...) but this will make future development easier since the exception is handled in one place.

You don't handle it with Doctrine... You handle with your coding language. If you're using PHP:

try {
  SomeClass::do_something(parameter);
} catch(DoctrineException $e) {
  show_error($e->getMessage());
}

This is obviously sudo-code, but it gives you an idea... You could show a general error or be specific and display doctrine's message...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM