Skip to content

Commit e40a210

Browse files
author
joseph lenton
committed
JosephLenton#19 minor exception display improvements
* exception stack grabbing code is now moved into it's own function * exceptions are now stated with prefix 'exception' on the stack trace * the main header now preserves whitespace
1 parent 51cf157 commit e40a210

File tree

2 files changed

+76
-46
lines changed

2 files changed

+76
-46
lines changed

example/multiple_exceptions.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ function c() {
1919
}
2020

2121
function d() {
22+
try {
23+
e();
24+
} catch ( Exception $ex ) {
25+
throw new Exception( "thrown exception", 0, $ex );
26+
}
27+
}
28+
29+
function e() {
30+
f();
31+
}
32+
33+
function f() {
2234
throw new Exception( 'blah' );
2335
}
2436

src/php_error.php

Lines changed: 64 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,7 +2124,9 @@ private function parseStackTrace( $code, $message, $errLine, $errFile, &$stackTr
21242124
$ex = $trace['exception'];
21252125

21262126
$exHtml = '<tr class="error-stack-trace-exception"><td>' .
2127+
'exception &quot;' .
21272128
htmlspecialchars( $ex->getMessage() ) .
2129+
'&quot;' .
21282130
'</td></tr>';
21292131
} else {
21302132
$exHtml = '';
@@ -2215,52 +2217,8 @@ public function reportError( $code, $message, $errLine, $errFile, $ex=null ) {
22152217
if ( $_php_error_is_ini_enabled ) {
22162218
$root = $this->applicationRoot;
22172219

2218-
if ( $ex !== null ) {
2219-
$next = $ex;
2220-
$stackTrace = array();
2221-
$skipStacks = 0;
2222-
2223-
for (
2224-
$next = $ex;
2225-
$next !== null;
2226-
$next = $next->getPrevious()
2227-
) {
2228-
$ex = $next;
2229-
2230-
$stack = $ex->getTrace();
2231-
$file = $ex->getFile();
2232-
$line = $ex->getLine();
2233-
2234-
//if ( count($stackTrace) > 0 ) {
2235-
$stack = array_slice( $stack, 0, count($stack)-count($stackTrace) + 1 );
2236-
//}
2237-
2238-
if ( count($stack) > 0 && (
2239-
!isset($stack[0]['file']) ||
2240-
!isset($stack[0]['line']) ||
2241-
$stack[0]['file'] !== $file ||
2242-
$stack[0]['line'] !== $line
2243-
) ) {
2244-
array_unshift( $stack, array(
2245-
'file' => $file,
2246-
'line' => $line
2247-
) );
2248-
}
2249-
2250-
$stackTrace = array_merge( $stack, $stackTrace );
2251-
if ( count($stackTrace) > 0 ) {
2252-
$stackTrace[0]['exception'] = $ex;
2253-
}
2254-
}
2255-
2256-
$message = $ex->getMessage();
2257-
$errFile = $ex->getFile();
2258-
$errLine = $ex->getLine();
2259-
2260-
$code = method_exists($ex, 'getSeverity') ?
2261-
$ex->getSeverity() :
2262-
$ex->getCode() ;
2263-
}
2220+
list( $ex, $stackTrace, $code, $errFile, $errLine ) =
2221+
$this->getStackTrace( $ex, $code, $errFile, $errLine );
22642222

22652223
list( $message, $srcErrFile, $srcErrLine, $altInfo ) =
22662224
$this->improveErrorMessage(
@@ -2313,6 +2271,62 @@ public function reportError( $code, $message, $errLine, $errFile, $ex=null ) {
23132271
}
23142272
}
23152273

2274+
private function getStackTrace( $ex, $code, $errFile, $errLine ) {
2275+
$stackTrace = null;
2276+
2277+
if ( $ex !== null ) {
2278+
$next = $ex;
2279+
$stackTrace = array();
2280+
$skipStacks = 0;
2281+
2282+
for (
2283+
$next = $ex;
2284+
$next !== null;
2285+
$next = $next->getPrevious()
2286+
) {
2287+
$ex = $next;
2288+
2289+
$stack = $ex->getTrace();
2290+
$file = $ex->getFile();
2291+
$line = $ex->getLine();
2292+
2293+
if ( $stackTrace !== null && count($stackTrace) > 0 ) {
2294+
$stack = array_slice( $stack, 0, count($stack)-count($stackTrace) + 1 );
2295+
}
2296+
2297+
if ( count($stack) > 0 && (
2298+
!isset($stack[0]['file']) ||
2299+
!isset($stack[0]['line']) ||
2300+
$stack[0]['file'] !== $file ||
2301+
$stack[0]['line'] !== $line
2302+
) ) {
2303+
array_unshift( $stack, array(
2304+
'file' => $file,
2305+
'line' => $line
2306+
) );
2307+
}
2308+
2309+
$stackTrace = ( $stackTrace !== null ) ?
2310+
array_merge( $stack, $stackTrace ) :
2311+
$stack ;
2312+
2313+
if ( count($stackTrace) > 0 ) {
2314+
$stackTrace[0]['exception'] = $ex;
2315+
}
2316+
}
2317+
2318+
$message = $ex->getMessage();
2319+
$errFile = $ex->getFile();
2320+
$errLine = $ex->getLine();
2321+
2322+
$code = method_exists($ex, 'getSeverity') ?
2323+
$ex->getSeverity() :
2324+
$ex->getCode() ;
2325+
}
2326+
2327+
return array( $ex, $stackTrace, $code, $errFile, $errLine );
2328+
}
2329+
23162330
private function generateDumpHTML( $arrays, $request, $response, $server ) {
23172331
$arrToHtml = function( $name, $array, $css='' ) {
23182332
$max = 0;
@@ -3291,6 +3305,10 @@ function displayHTML( Closure $head, $body=null, $javascript=null ) {
32913305
background: #aa4040;
32923306
}
32933307

3308+
#error-title {
3309+
white-space: pre-wrap;
3310+
}
3311+
32943312
<?php
32953313
/*
32963314
* Error Background Text.

0 commit comments

Comments
 (0)