parser

Написать ответ на текущее сообщение

 

 
   команды управления поиском

CustomException.p

tezro 26.05.2009 19:14 / 26.05.2009 19:20

В общем, как-то так.
###############################################################################
# Custom exceptions handling, version 0.1
###############################################################################

@CLASS
CustomException



###############################################################################
@auto[]
$response:content-type[
	$.value[text/html]
	$.charset[$response:charset]
]
^if($env:REMOTE_ADDR eq '127.0.0.1'){
	$bShow(true)
}{
	$bShow(false)
	^if($cookie:debug == 1){
		$bShow(true)
		^if(def $form:undebug || $form:nameless eq 'undebug'){
			$bShow(false)
			$cookie:debug[
				$.value(0)
				$.expires[session]
			]
		}
	}{
		^if(def $form:debug || $form:nameless eq 'debug'){
			$bShow(true)
			$cookie:debug[
				$.value(1)
				$.expires[session]
			]
		}
	}
}
$result[]
#end @auto[]



###############################################################################
@show[hException;tStack;tClassPath]
^if($self.bShow){
	^if(!def $tClassPath){
		$tClassPath[$MAIN:CLASS_PATH]
	}
	$result[^CustomException:customHandler[$hException;$tStack;$tClassPath]]
}{
	$result[^CustomException:defaultHandler[]]
}
#end @show[]



###############################################################################
@customHandler[hException;tStack;tClassPath][sExceptionFile;tExceptionFile;iExceptionLine;iExceptionColumn;sLocatedPath;sTrimmedPath;sLine;tNew]
<html>
<head>
	<title>Unhandled exception.</title>
	<style type="text/css">
		body {margin:0; padding:0; font:normal 0.8em Tahoma, Arial}
		h1 {margin:0.75em 0 0.3em 0; padding:0; font:normal 2em Arial; color:white}
		h2 {margin:0.75em 0 0.3em 0; padding:0; font:normal 1.75em Arial; color:white}
		h3 {margin:1em 0 0.5em 0; padding:0; font:bold 1em Tahoma; color:white}
		p {margin:0}
		pre {font-size:1.25em; margin:0}
		b.nb {color:#444; background-color:#fc0; padding:0 0.2em; font-weight:normal}
		span.tab {background-color:#777; margin:0 0.1em; padding:0 1em; color:#aaa}
		table, td, tr {padding:0; margin:0; vertical-align:top; border:0; border-collapse:collapse}
		div.title {padding:0.5em 5% 2em; background-color:#69c; color:white}
		div.comment {padding:0.5em 5% 2em; background-color:#777; color:white}
		div.body {padding:0.5em 5% 2em; background-color:#aaa}
		table.data {font-size:0.95em; color:white}
		table.data tr.even {background-color:#959595}
		table.data tr.odd {background-color:#555}
		table.data tr.nb {background-color:#fc0; color:#444}
		table.data td {padding:0.25em 0}
		table.data td.number {padding:0.25em 0.5em}
		table.data td.line {text-align:right}
		table.data td.column {padding-right:0.5em}
		table.data td.center {white-space:nowrap; padding:0.25em 1em}
	</style>
</head>
<body>
	<div class="title">
		<h1>Error occured at &ldquo;$request:uri&rdquo;</h1>
		^if(def $hException.source){
			<p>Source &mdash^; ${hException.source}</p>
		}
		^if(def $hException.type){
			<p>Exception type: <b>$hException.type</b></p>
		}
		^if(def $hException.file){
			<p>Exception location: <b>$hException.file</b> in line <b>$hException.lineno</b> at char <b>$hException.colno</b></p>
		}
	</div>
	<div class="comment">
		<h2>Generic comment</h2>
		^tClassPath.menu{
			^if(^hException.comment.pos[$tClassPath.path] > 0 && ^tClassPath.path.length[] > ^sLocatedPath.length[]){
				$sLocatedPath[$tClassPath.path]
				$sTrimmedPath[^hException.comment.mid(^hException.comment.pos[$tClassPath.path])]
				^sTrimmedPath.match[(${sLocatedPath}.+)\((\d+)\:(\d+)\).*][g]{
					$sExceptionFilePath[$match.1]
					$iExceptionLine($match.2)
					$iExceptionColumn($match.3)
				}
			}
		}
		^if(def $sExceptionFilePath){
			<p>^untaint[html]{^hException.comment.match[\/([^^\/]*?)\(][]{/<b class="nb">$match.1</b> (}}</p>
		}{
			$sExceptionFilePath[^hException.file.mid(^request:document-root.length[])]
			$iExceptionLine($hException.lineno)
			$iExceptionColumn($hException.colno)

			<p>^untaint[html]{^hException.file.match[\/([^^\/]*?)^$][]{/<b class="nb">$match.1</b>}} (${iExceptionLine}:$iExceptionColumn): $hException.comment</p>
		}
		^try{
#			load the needed lines from file as table
			$fExceptionFile[^file::load[text;$sExceptionFilePath]]
			$tExceptionFile[^fExceptionFile.text.split[^#0A]]
			$tNew[^table::create{lineNumber	lineValue}]
			^tExceptionFile.menu{
				^tNew.append{^tExceptionFile.line[]	^taint[html][$tExceptionFile.piece]}
			}
			$tExceptionFile[^tNew.select(^tNew.line[] >= $iExceptionLine - 5 && ^tNew.line[] <= $iExceptionLine)]

#			define just a file name
			$sExceptionFile[^sExceptionFilePath.split[/;rh]]
			$sExceptionFile[$sExceptionFile.0]
#			print out
			<h3>Lines $tExceptionFile.lineNumber&mdash^;^tExceptionFile.offset[set](-1) $tExceptionFile.lineNumber from &ldquo^;$sExceptionFile&rdquo^;</h3>
			<table class="data">
			^tExceptionFile.menu{
				<tr class="^if(^tExceptionFile.offset[] % 2){even}{odd}^if(^tExceptionFile.line[] == ^tExceptionFile.count[]){ nb}">
					<td class="number">$tExceptionFile.lineNumber</td>
					<td class="center">
						$sLine[^taint[html][^tExceptionFile.lineValue.match[\t][g]{%%%TA6%%%}]]
						$sLine[^sLine.match[%%%TA6%%%][g]{<span class="tab">TAB</span>}]
						$sLine&para^;
					</td>
				</tr>
			}
			</table>
		}{
			$exception.handled(1)
		}
	</div>
	<div class="body">
		^if($tStack){
			<h2>Stack review</h2>
			<table class="data">
			^tStack.menu{
				<tr^if(^tStack.offset[] % 2){ class="even"}>
					<td class="number">^eval(^tStack.count[] - ^tStack.offset[]).</td>
					<td>$tStack.name</td>
					<td class="center">$tStack.file</td>
					<td class="line">$tStack.lineno</td>
					<td>:</td>
					<td class="column">$tStack.colno</td>
				</tr>
			}
			</table>
		}{
			<h2>No stack available</h2>
		}
	</div>
</body>
</html>
#end @customException[]



###############################################################################
@defaultHandler[][dDate]
<html>
	<head>
		<title>500 error at $env:SERVER_NAME</title>
		<style type="text/css">
			body {margin:0; padding:0; font:normal 0.8em Tahoma, Arial}
			h1 {margin:0.75em 0 0.3em 0; padding:0; font:normal 2em Arial; color:white}
			div.title {padding:0.5em 5% 2em; background-color:#69c; color:white}
			div.body {padding:0.5em 5%; background-color:#aaa; color:white}
		</style>
	</head>
<body>
	<div class="title">
		<h1>Server &ldquo;$env:SERVER_NAME&rdquo; is down.</h1>
		<p>It's either being maintenanced or not working properly. Come back soon.</p>
	</div>
	<div class="body">
		$dDate[^date::now[]]
		$env:SERVER_NAME at $env:SERVER_ADDR &mdash; ^dDate.sql-string[].
	</div>
</body>
</html>
#end @defaultHandler[]
Ну и вызов:
###############################################################################
@unhandled_exception[e;s]
^use[CustomException.p]
$result[^CustomException:show[$e;$s;$MAIN:CLASS_PATH]]
#end @unhandled_exception[]