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.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: /**
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: $env = $this->app->environment();
70: $env['slim.log']->error($e);
71: $this->app->contentType('text/html');
72: $this->app->response()->status(500);
73: $this->app->response()->body($this->renderBody($env, $e));
74: }
75: }
76:
77: /**
78: * Render response body
79: * @param array $env
80: * @param \Exception $exception
81: * @return string
82: */
83: protected function renderBody(&$env, $exception)
84: {
85: $title = 'Slim Application Error';
86: $code = $exception->getCode();
87: $message = $exception->getMessage();
88: $file = $exception->getFile();
89: $line = $exception->getLine();
90: $trace = $exception->getTraceAsString();
91: $html = sprintf('<h1>%s</h1>', $title);
92: $html .= '<p>The application could not run because of the following error:</p>';
93: $html .= '<h2>Details</h2>';
94: $html .= sprintf('<div><strong>Type:</strong> %s</div>', get_class($exception));
95: if ($code) {
96: $html .= sprintf('<div><strong>Code:</strong> %s</div>', $code);
97: }
98: if ($message) {
99: $html .= sprintf('<div><strong>Message:</strong> %s</div>', $message);
100: }
101: if ($file) {
102: $html .= sprintf('<div><strong>File:</strong> %s</div>', $file);
103: }
104: if ($line) {
105: $html .= sprintf('<div><strong>Line:</strong> %s</div>', $line);
106: }
107: if ($trace) {
108: $html .= '<h2>Trace</h2>';
109: $html .= sprintf('<pre>%s</pre>', $trace);
110: }
111:
112: 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);
113: }
114: }
115: