PrettyExceptions.php (4099B)
1 <?php 2 /** 3 * Slim - a micro PHP 5 framework 4 * 5 * @author Josh Lockhart <info@slimframework.com> 6 * @copyright 2011 Josh Lockhart 7 * @link http://www.slimframework.com 8 * @license http://www.slimframework.com/license 9 * @version 2.4.2 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 /** 36 * Pretty Exceptions 37 * 38 * This middleware catches any Exception thrown by the surrounded 39 * application and displays a developer-friendly diagnostic screen. 40 * 41 * @package Slim 42 * @author Josh Lockhart 43 * @since 1.0.0 44 */ 45 class PrettyExceptions extends \Slim\Middleware 46 { 47 /** 48 * @var array 49 */ 50 protected $settings; 51 52 /** 53 * Constructor 54 * @param array $settings 55 */ 56 public function __construct($settings = array()) 57 { 58 $this->settings = $settings; 59 } 60 61 /** 62 * Call 63 */ 64 public function call() 65 { 66 try { 67 $this->next->call(); 68 } catch (\Exception $e) { 69 $log = $this->app->getLog(); // Force Slim to append log to env if not already 70 $env = $this->app->environment(); 71 $env['slim.log'] = $log; 72 $env['slim.log']->error($e); 73 $this->app->contentType('text/html'); 74 $this->app->response()->status(500); 75 $this->app->response()->body($this->renderBody($env, $e)); 76 } 77 } 78 79 /** 80 * Render response body 81 * @param array $env 82 * @param \Exception $exception 83 * @return string 84 */ 85 protected function renderBody(&$env, $exception) 86 { 87 $title = 'Slim Application Error'; 88 $code = $exception->getCode(); 89 $message = $exception->getMessage(); 90 $file = $exception->getFile(); 91 $line = $exception->getLine(); 92 $trace = str_replace(array('#', "\n"), array('<div>#', '</div>'), $exception->getTraceAsString()); 93 $html = sprintf('<h1>%s</h1>', $title); 94 $html .= '<p>The application could not run because of the following error:</p>'; 95 $html .= '<h2>Details</h2>'; 96 $html .= sprintf('<div><strong>Type:</strong> %s</div>', get_class($exception)); 97 if ($code) { 98 $html .= sprintf('<div><strong>Code:</strong> %s</div>', $code); 99 } 100 if ($message) { 101 $html .= sprintf('<div><strong>Message:</strong> %s</div>', $message); 102 } 103 if ($file) { 104 $html .= sprintf('<div><strong>File:</strong> %s</div>', $file); 105 } 106 if ($line) { 107 $html .= sprintf('<div><strong>Line:</strong> %s</div>', $line); 108 } 109 if ($trace) { 110 $html .= '<h2>Trace</h2>'; 111 $html .= sprintf('<pre>%s</pre>', $trace); 112 } 113 114 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); 115 } 116 }