|View discussion [4 posts]||View script||License||Download documentation as: HTML or editable|
|Download script||History||Other scripts by: sunanda|
Script Library: 1222 scripts
Documentation for: cgi-debug.r
Sunanda November 2003
A version of this script is used in all REBOL.org CGI scripts. It is a wrapper that reports any errors that occur when bad things happen to our scripts. It does three things:
A typical Library Script looks something like this
#!shebang path to rebol.exe -cs REBOL [header] do %cgi-debug.r cgi-debug [ -- whole script here quit ]
This script will fail about every second time you call it:
#!/..../rebol.exe -cs REBOL ["header" ] do %lib-cgi-debug.r cgi-debug [ random/seed now/precise if (random/secure 10) > 5 [ 0 / 0] print "Content-type: text/html^/" print "<html><head></head><body>" print "<h1>It worked this time!!</h1>" print "</body></html>" quit ] ;; cgi-debug
Sometimes we need to do some tidying-up after we've sent the HTML page. If so, any error should be invisible to the user -- after all, what they wanted has happened, If so we wrap that part with the /silent refinement. It'll still log an error and send an email, but the user won't see the problem.
#!shebang path to rebol.exe -cs REBOL [header] do %cgi-debug.r cgi-debug [ -- most of the script here ] cgi-debug/silent [ -- "invisible" finalisation here quit ]
Why the 1 after the do?
It's possible that the target code does not return a value. If so, the error?/try will fail. The 1 makes sure we always get a value back from the block.
An earlier version of the script had:
[do target-code true]
But (as pointed out by Anton on the REBOL mailing list), the target-code could have unset 'true -- unlikely maybe; and if it's doing things like that, it could screw us up by unsetting (or redefining) 'error? and 'if and all sorts of other things -- but still, this extra safety check doesn't cost anything.
Is the name of the script being executed.
The getting of the ip-address is wrapped in an error/try because, for testing, it can sometimes make sense to run the script from the REBOL console, not via a webserver:
You probably don't want to be sending alert messages from you local testing system.
Set to your sending-email address and default mail server
Replace with the email address of whoever will be dealing with the problem.