View discussion [131 posts] | View script | License | Download documentation as: HTML or editable |
Download script | History | Other scripts by: sunanda |
[0.055] 14.655k
Documentation for: cgi-debug.rcgi-debug functionSunanda November 2003 Contents1. PurposeA 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:
2. UsageA 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 ] 3. ExampleThis 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 4. Notes4.1 Silent refinementSometimes 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 ] 4.2 if error? cgi-debug-capture-error: try [do target-code 1]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. 4.3 system/options/scriptIs the name of the script being executed. 4.4 ip-address: noneThe 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:
4.5 if not 127.0.0.1 = to-tuple system/options/cgi/remote-addrYou probably don't want to be sending alert messages from you local testing system. 4.6 set-net [--from email address-- --default-server--]Set to your sending-email address and default mail server 4.7 send to-email "--to-email address--"Replace with the email address of whoever will be dealing with the problem. Notes
|