PrettyExceptions.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Slim - a micro PHP 5 framework
  4. *
  5. * @author Josh Lockhart <[email protected]>
  6. * @copyright 2011 Josh Lockhart
  7. * @link http://www.slimframework.com
  8. * @license http://www.slimframework.com/license
  9. * @version 2.2.0
  10. * @package Slim
  11. *
  12. * MIT LICENSE
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining
  15. * a copy of this software and associated documentation files (the
  16. * "Software"), to deal in the Software without restriction, including
  17. * without limitation the rights to use, copy, modify, merge, publish,
  18. * distribute, sublicense, and/or sell copies of the Software, and to
  19. * permit persons to whom the Software is furnished to do so, subject to
  20. * the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be
  23. * included in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. */
  33. namespace Slim\Middleware;
  34. /**
  35. * Pretty Exceptions
  36. *
  37. * This middleware catches any Exception thrown by the surrounded
  38. * application and displays a developer-friendly diagnostic screen.
  39. *
  40. * @package Slim
  41. * @author Josh Lockhart
  42. * @since 1.0.0
  43. */
  44. class PrettyExceptions extends \Slim\Middleware
  45. {
  46. /**
  47. * @var array
  48. */
  49. protected $settings;
  50. /**
  51. * Constructor
  52. * @param array $settings
  53. */
  54. public function __construct($settings = array())
  55. {
  56. $this->settings = $settings;
  57. }
  58. /**
  59. * Call
  60. */
  61. public function call()
  62. {
  63. try {
  64. $this->next->call();
  65. } catch (\Exception $e) {
  66. $env = $this->app->environment();
  67. $env['slim.log']->error($e);
  68. $this->app->contentType('text/html');
  69. $this->app->response()->status(500);
  70. $this->app->response()->body($this->renderBody($env, $e));
  71. }
  72. }
  73. /**
  74. * Render response body
  75. * @param array $env
  76. * @param \Exception $exception
  77. * @return string
  78. */
  79. protected function renderBody(&$env, $exception)
  80. {
  81. $title = 'Slim Application Error';
  82. $code = $exception->getCode();
  83. $message = $exception->getMessage();
  84. $file = $exception->getFile();
  85. $line = $exception->getLine();
  86. $trace = $exception->getTraceAsString();
  87. $html = sprintf('<h1>%s</h1>', $title);
  88. $html .= '<p>The application could not run because of the following error:</p>';
  89. $html .= '<h2>Details</h2>';
  90. $html .= sprintf('<div><strong>Type:</strong> %s</div>', get_class($exception));
  91. if ($code) {
  92. $html .= sprintf('<div><strong>Code:</strong> %s</div>', $code);
  93. }
  94. if ($message) {
  95. $html .= sprintf('<div><strong>Message:</strong> %s</div>', $message);
  96. }
  97. if ($file) {
  98. $html .= sprintf('<div><strong>File:</strong> %s</div>', $file);
  99. }
  100. if ($line) {
  101. $html .= sprintf('<div><strong>Line:</strong> %s</div>', $line);
  102. }
  103. if ($trace) {
  104. $html .= '<h2>Trace</h2>';
  105. $html .= sprintf('<pre>%s</pre>', $trace);
  106. }
  107. return sprintf("<html><head><title>%s</title><style>body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;}h1{margin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{display:inline-block;width:65px;}</style></head><body>%s</body></html>", $title, $html);
  108. }
  109. }