Skip to content

Commit cd105cf

Browse files
author
sasou2008
committed
优化异常抛出;
1 parent f8e4c8a commit cd105cf

File tree

2 files changed

+47
-32
lines changed

2 files changed

+47
-32
lines changed

demo/application/Views/error.php

+30-31
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,67 @@
55
{
66
$lines = file( $file );
77
}
8-
if (\Gene\Application::getEnvironment()) {
9-
//echo 'Exception Log!';
10-
//die;
11-
}
128
?>
139
<!DOCTYPE html>
1410
<head>
1511
<meta charset="utf-8">
1612
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
17-
<title><?php echo $e->getMessage(); ?></title>
13+
<title>Caught Exception</title>
1814
<style type="text/css">
1915
body{padding: 0;margin: 0;}
20-
.content {width: 960px;margin: 0 auto;padding:5px 10px;}
21-
h1, h2{font-weight: bold;color: #AF7C8C;}
22-
ul.code {font-size: 13px;line-height: 20px;color: #68777d;background-color: #f2f9fc;border: 1px solid #c9e6f2;border-radius: 3px;}
16+
.content {width: 80%;margin: 0 auto;padding:5px 10px;}
17+
h1{font-weight: bolder;color: #cc7a94;padding: 5px;}
18+
h2{font-weight: normal;color: #AF7C8C;background-color: #e9f4f5;padding: 5px;}
19+
ul.code {font-size: 13px;line-height: 20px;color: #68777d;background-color: #f2f9fc;border: 1px solid #c9e6f2;}
2320
ul.code li {width : 100%;margin:0px;white-space: pre ;list-style-type:none;font-family : monospace;}
2421
ul.code li.line {color : red;}
25-
table.trace {width : 100%;font-size: 13px;line-height: 20px;color: #68777d;background-color: #f2f9fc;border: 1px solid #c9e6f2;border-radius: 3px;}
26-
table.thead tr {background : rgb(240,240,240);}
27-
table.trace tr.odd {background : #DAEFE9;}
28-
table.trace tr.even {background : #ECF1F0;}
29-
table.trace td {padding : 2px 4px 2px 4px;}
30-
.foot {line-height: 20px;color: #D2D8DA;margin:10px;}
22+
table.trace {width : 100%;font-size: 13px;line-height: 20px;color: #247696;background-color: #c9e6f2;}
23+
table.trace thead tr {background-color: #90a9b3;color: white;}
24+
table.trace tr {background-color: #f2f9fc;}
25+
table.trace td {padding : 5px;}
26+
.foot {line-height: 20px;color: #cc7a94;margin:10px;}
3127
</style>
3228
</head>
3329
<body>
3430
<div class="content">
35-
<center><h1>Uncaught <?php echo get_class($e); ?></h1></center>
36-
<h2><?php echo $e->getMessage(); ?></h2>
37-
<p>An uncaught <?php echo get_class($e); ?> was thrown on line <?php echo $line;?> of file <?php echo basename( $file ); ?> that prevented further execution of this request.</p>
38-
<h2>Where it happened:</h2>
31+
32+
<center><h1>Caught Exception</h1></center>
33+
34+
<h2>What's happened:</h2>
35+
<code><?php echo $e->getMessage(); ?></code>
36+
37+
<h2>Where's happened:</h2>
3938
<?php if(isset($lines)) { ?>
4039
<code>File:***<?php echo substr($file,-(ceil(strlen($file)*0.6))); ?> Line:<?php echo $line; ?></code>
4140
<ul class="code">
42-
<?php for($i=$line-3;$i<$line+3;$i++) { ?>
41+
<?php for($i=$line-8;$i<$line+8;$i++) { ?>
4342
<?php if($i>0 && $i<count($lines)) { ?>
4443
<?php if($i == $line-1) { ?>
45-
<li class="line"><?php echo str_replace( "\n", "", $lines[$i] ); ?></li>
44+
<li class="line"><?php echo $lines[$i]; ?></li>
4645
<?php } else { ?>
47-
<li><?php echo str_replace( "\n", "", $lines[$i] ); ?></li>
46+
<li><?php echo $lines[$i]; ?></li>
4847
<?php } ?>
4948
<?php } ?>
5049
<?php } ?>
5150
</ul>
5251
<?php } ?>
5352
<?php if(is_array( $e->getTrace() )) { ?>
53+
5454
<h2>Stack trace:</h2>
5555
<table class="trace">
56-
<thead><tr><td>File</td><td>Line</td><td>Class</td><td>Function</td><td>Arguments</td></tr></thead>
56+
<thead><tr><td width="180px">File</td><td width="30px">Line</td><td width="250px">Class</td><td width="150px">Function</td><td>Arguments</td></tr></thead>
5757
<tbody>
5858
<?php if(is_array($e->getTrace())) { foreach($e->getTrace() as $i => $trace) { ?>
59-
<?php if(isset($trace[ 'class' ])) { ?>
60-
<tr class="<?php echo $i % 2 == 0 ? 'even' : 'odd'; ?>">
59+
<?php if(isset($trace[ 'class' ]) && $trace[ 'function' ] != "doError") { ?>
60+
<tr>
6161
<td><?php echo isset($trace[ 'file' ]) ? basename($trace[ 'file' ]) : ''; ?></td>
62-
<td><?php echo isset($trace[ 'line' ]) ? $trace[ 'line' ] : ''; ?></td>
63-
<td><?php echo isset($trace[ 'class' ]) ? $trace[ 'class' ] : ''; ?></td>
64-
<td><?php echo isset($trace[ 'function' ]) ? $trace[ 'function' ] : ''; ?></td>
62+
<td><?php echo $trace[ 'line' ] ?? ''; ?></td>
63+
<td><?php echo $trace[ 'class' ] ?? ''; ?></td>
64+
<td><?php echo $trace[ 'function' ] ?? ''; ?></td>
6565
<td>
6666
<?php if(isset($trace['args'])) { ?>
6767
<?php if(is_array($trace['args'])) { foreach($trace['args'] as $i => $arg) { ?>
68-
<span title="<?php echo var_export( $arg, true ); ?>"><?php echo gettype( $arg ); ?></span>
69-
<?php echo $i < count( $trace['args'] ) -1 ? ',' : ''; ?>
68+
<span title="<?php echo var_export( $arg, true ); ?>"><?php echo gettype( $arg ); ?></span>
7069
<?php } } ?>
7170
<?php } else { ?>
7271
NULL
@@ -77,10 +76,10 @@
7776
<?php } } ?>
7877
</tbody>
7978
</table>
80-
<?php } else { ?>
81-
<pre><?php echo $e->getTraceAsString(); ?></pre>
8279
<?php } ?>
80+
8381
<center class="foot">Gene <?php echo gene_version(); ?></center>
82+
8483
</div>
8584
</body>
8685
</html>

src/exception/exception.c

+17-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ ZEND_BEGIN_ARG_INFO_EX(gene_exception_exception, 0, 0, 0)
4040
ZEND_ARG_INFO(0, callback)
4141
ZEND_END_ARG_INFO()
4242

43+
ZEND_BEGIN_ARG_INFO_EX(gene_exception_do_exception, 0, 0, 1)
44+
ZEND_ARG_INFO(0, ex)
45+
ZEND_END_ARG_INFO()
46+
4347
ZEND_BEGIN_ARG_INFO_EX(gene_exception_do_error, 0, 0, 2)
4448
ZEND_ARG_INFO(0, code)
4549
ZEND_ARG_INFO(0, msg)
@@ -190,7 +194,18 @@ PHP_METHOD(gene_exception, doError) {
190194
RETURN_NULL();
191195
}
192196
gene_trigger_error(code, "%s", ZSTR_VAL(msg));
193-
RETURN_TRUE;
197+
}
198+
/* }}} */
199+
200+
/*
201+
* {{{ gene_exception::doException
202+
*/
203+
PHP_METHOD(gene_exception, doException) {
204+
zval *ex = NULL;
205+
if (zend_parse_parameters(ZEND_NUM_ARGS()TSRMLS_CC, "z", &ex) == FAILURE) {
206+
RETURN_NULL();
207+
}
208+
gene_view_display("error" TSRMLS_CC);
194209
}
195210
/* }}} */
196211

@@ -200,6 +215,7 @@ PHP_METHOD(gene_exception, doError) {
200215
zend_function_entry gene_exception_methods[] = {
201216
PHP_ME(gene_exception, setErrorHandler, gene_exception_error, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
202217
PHP_ME(gene_exception, setExceptionHandler, gene_exception_exception, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
218+
PHP_ME(gene_exception, doException, gene_exception_do_exception, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
203219
PHP_ME(gene_exception, doError, gene_exception_do_error, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
204220
#if ((PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 3)) || (PHP_MAJOR_VERSION < 5)
205221
PHP_ME(gene_exception, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)

0 commit comments

Comments
 (0)