PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/phocoa/framework/WFExceptionReporting.php

https://github.com/SwissalpS/phocoa
PHP | 99 lines | 56 code | 6 blank | 37 comment | 3 complexity | a014e021411855620d0acda53770b715 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4. * @package framework-base
  5. * @subpackage Error
  6. * @copyright Copyright (c) 2005 Alan Pinstein. All Rights Reserved.
  7. * @version $Id: kvcoding.php,v 1.3 2004/12/12 02:44:09 alanpinstein Exp $
  8. * @author Alan Pinstein <apinstein@mac.com>
  9. */
  10. /**
  11. * The WFExceptionReporting class provides some static helper methods for dealing with exceptions.
  12. */
  13. class WFExceptionReporting extends WFObject
  14. {
  15. /**
  16. * Log the passed exception to the framework's log folder.
  17. */
  18. static function log(Exception $e)
  19. {
  20. $logfile = WFWebApplication::appDirPath(WFWebApplication::DIR_LOG) . '/framework_exceptions.log';
  21. $smarty = new WFSmarty();
  22. $smarty->assign('exception', $e);
  23. $smarty->setTemplate(WFWebApplication::appDirPath(WFWebApplication::DIR_SMARTY) . '/app_error_log.tpl');
  24. $errText = $smarty->render(false);
  25. // append info to log
  26. $fs = fopen($logfile, 'a');
  27. fputs($fs, $errText);
  28. fclose($fs);
  29. }
  30. /**
  31. * Utility function to produce a standardized error reporting format.
  32. *
  33. * This function will walk up the entire Exception tree and also append the most recent error_get_last().
  34. *
  35. * @param object Exception
  36. * @return array A standardized array structure of all errors:
  37. * [
  38. * {
  39. * 'title' => '',
  40. * 'message' => '',
  41. * 'code' => '',
  42. * 'trace' => ''
  43. * }, ...
  44. * ]
  45. */
  46. public static function generatedStandardizedErrorDataFromException(Exception $e)
  47. {
  48. // build stack of errors (php 5.3+)
  49. if (method_exists($e, 'getPrevious'))
  50. {
  51. $allExceptions = array();
  52. do {
  53. $allExceptions[] = $e;
  54. } while ($e = $e->getPrevious());
  55. }
  56. else
  57. {
  58. $allExceptions = array($e);
  59. }
  60. $errorData = array();
  61. foreach ($allExceptions as $e) {
  62. $errorData[] = array(
  63. 'title' => get_class($e),
  64. 'message' => $e->getMessage(),
  65. 'code' => $e->getCode(),
  66. 'trace' => array_merge(
  67. array("At {$e->getFile()}:{$e->getLine()}"),
  68. explode("\n", $e->getTraceAsString())
  69. )
  70. );
  71. }
  72. $lastErr = error_get_last();
  73. if ($lastErr)
  74. {
  75. extract($lastErr);
  76. $errorData[] = array(
  77. 'title' => "error_get_last(): most recent; may or may not be relevant.",
  78. 'message' => $message,
  79. 'code' => $type,
  80. 'trace' => "{$file}:{$line}"
  81. );
  82. }
  83. return $errorData;
  84. }
  85. /**
  86. * Mail the passed exception to the framework's log folder.
  87. * @todo Not yet implemented! Where to get email address from? probably WFWebApplicationDelegate, overridable by param
  88. * Also need to write WFSmartyMail class?
  89. */
  90. function mail(Exception $e, $email = NULL)
  91. {
  92. }
  93. }