@@ -22,73 +22,98 @@ class JErrorPage
22
22
/**
23
23
* Render the error page based on an exception.
24
24
*
25
- * @param Exception $error The exception for which to render the error page.
25
+ * @param object $error An Exception or Throwable (PHP 7+) object for which to render the error page.
26
26
*
27
27
* @return void
28
28
*
29
29
* @since 3.0
30
30
*/
31
- public static function render (Exception $ error )
31
+ public static function render ($ error )
32
32
{
33
- try
33
+ $ expectedClass = PHP_MAJOR_VERSION >= 7 ? 'Throwable ' : 'Exception ' ;
34
+ $ isException = $ error instanceof $ expectedClass ;
35
+
36
+ // In PHP 5, the $error object should be an instance of Exception; PHP 7 should be a Throwable implementation
37
+ if ($ isException )
34
38
{
35
- $ app = JFactory::getApplication ();
36
- $ document = JDocument::getInstance ('error ' );
37
-
38
- $ code = $ error ->getCode ();
39
- if (!isset (JHttpResponse::$ status_messages [$ code ])) {
40
- $ code = '500 ' ;
41
- }
42
-
43
- if (ini_get ('display_errors ' )) {
44
- $ message = $ error ->getMessage ();
45
- } else {
46
- $ message = JHttpResponse::$ status_messages [$ code ];
47
- }
48
-
49
- // Exit immediatly if we are in a CLI environment
50
- if (!$ document || PHP_SAPI == 'cli ' )
39
+ try
51
40
{
52
- exit ($ message );
53
- $ app ->close (0 );
54
- }
41
+ $ app = JFactory::getApplication ();
42
+ $ document = JDocument::getInstance ('error ' );
55
43
56
- $ config = JFactory::getConfig ();
44
+ $ code = $ error ->getCode ();
45
+ if (!isset (JHttpResponse::$ status_messages [$ code ])) {
46
+ $ code = '500 ' ;
47
+ }
57
48
58
- // Get the current template from the application
59
- $ template = $ app ->getTemplate ();
49
+ if (ini_get ('display_errors ' )) {
50
+ $ message = $ error ->getMessage ();
51
+ } else {
52
+ $ message = JHttpResponse::$ status_messages [$ code ];
53
+ }
60
54
61
- $ document ->setError ($ error );
55
+ if (!$ document || PHP_SAPI == 'cli ' )
56
+ {
57
+ // We're probably in an CLI environment
58
+ jexit ($ message );
59
+ }
62
60
63
- if (ob_get_contents ()) {
64
- ob_end_clean ();
65
- }
61
+ // Get the current template from the application
62
+ $ template = $ app ->getTemplate ();
66
63
67
- $ document ->setTitle (JText::_ ('Error ' ) . ': ' . $ code );
68
- $ data = $ document ->render (
69
- false ,
70
- array ('template ' => $ template ,
71
- 'directory ' => JPATH_THEMES ,
72
- 'debug ' => $ config ->get ('debug ' ))
73
- );
64
+ // Push the error object into the document
65
+ $ document ->setError ($ error );
74
66
75
- // Failsafe to get the error displayed.
76
- if (!empty ($ data ))
77
- {
78
- // Do not allow cache
79
- $ app ->allowCache (false );
67
+ if (ob_get_contents ()) {
68
+ ob_end_clean ();
69
+ }
70
+
71
+ $ document ->setTitle (JText::_ ('Error ' ) . ': ' . $ code );
72
+ $ data = $ document ->render (
73
+ false ,
74
+ array (
75
+ 'template ' => $ template ,
76
+ 'directory ' => JPATH_THEMES ,
77
+ 'debug ' => JFactory::getConfig ()->get ('debug ' )
78
+ )
79
+ );
80
80
81
- $ app ->setBody ($ data );
82
- echo $ app ->toString ();
81
+ // Do not allow cache
82
+ $ app ->allowCache (false );
83
+
84
+ // If nothing was rendered, just use the message from the Exception
85
+ if (empty ($ data ))
86
+ {
87
+ $ data = $ message ;
88
+ }
89
+
90
+ $ app ->setBody ($ data );
91
+
92
+ echo $ app ->toString ();
93
+
94
+ return ;
95
+ }
96
+ catch (Exception $ e )
97
+ {
98
+ // Pass the error down
83
99
}
84
- else
85
- {
86
- exit ($ message );
87
- }
88
100
}
89
- catch (Exception $ e )
90
- {
91
- exit ('Error displaying the error page: ' . $ e ->getMessage () . ': ' . $ message );
101
+
102
+ // This isn't an Exception, we can't handle it.
103
+ if (!headers_sent ())
104
+ {
105
+ header ('HTTP/1.1 500 Internal Server Error ' );
92
106
}
107
+
108
+ $ message = 'Error displaying the error page ' ;
109
+
110
+ if ($ isException )
111
+ {
112
+ $ message .= ': ' . $ e ->getMessage () . ': ' . $ message ;
113
+ }
114
+
115
+ echo $ message ;
116
+
117
+ jexit (1 );
93
118
}
94
119
}
0 commit comments