Created
August 30, 2012 09:45
-
-
Save igorshubovych/3524989 to your computer and use it in GitHub Desktop.
Rules for logging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #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; | |
} |
Agree with the previous comment
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Throwing exceptions within the catch block? Isn't it better to write a logic to do something instead of just "shifting responsibility"?