Skip to content

Instantly share code, notes, and snippets.

@igorshubovych
Created August 30, 2012 09:45
Show Gist options
  • Save igorshubovych/3524989 to your computer and use it in GitHub Desktop.
Save igorshubovych/3524989 to your computer and use it in GitHub Desktop.
Rules for logging
// #1
// When you use Zend_Log class to log errors,
// please NEVER do this:
catch (Exception $e) {
Log::err($e->getMessage()); // WRONG!
// Lost stacktrace here
}
// Instead do:
catch (Exception $e) {
Log::err($e);
// Zend_Log knows how to display message and stacktrace
}
// We need this to save not only error message,
// but a stacktrace too.
// #2
// Also NEVER do this:
catch (\Exception $e) {
throw new \Exception('Can not do smth'); // WRONG!
// the reason of exception is lost
}
// Instead do:
catch (\Exception $e) {
throw new \Exception('Can not do smth because of '.$e->getMessage());
// The reason is saved for investigation
}
// In 1st case the reason of actual problem was lost.
// When something bad happens we need to know all the details.
// #3
// No need to do this
catch(Exception $e){
Log::err($e);
throw new Exception($e->getMessage() . ' on ' . $e->getLine() . ' in ' . $e->getFile());
}
// Just
catch(Exception $e){
Log::err($e);
throw $e;
}
@shybovycha
Copy link

Throwing exceptions within the catch block? Isn't it better to write a logic to do something instead of just "shifting responsibility"?

@dmitriy-kerest
Copy link

Agree with the previous comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment