Documention for: cgi-debug.r Created by: sunanda on: 24-Nov-2003 Last updated by: sunanda on: 26-Nov-2003 Format: text/editable Downloaded on: 19-Apr-2024 cgi-debug function Sunanda November 2003 =toc ===Purpose 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: * Apologise to the user for the problem * Write a report to an error log * Sends an email to notify a Librarian that we need to come running ===Usage 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 ] ===Example 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 ===Notes ---Silent refinement 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 ] ---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. ---system/options/script Is the name of the script being executed. ---ip-address: none 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: * If a script contains syntax errors (unmatched square brackets, for example), it won't get as far as cgi-debug to report an error. That can be mystifying. Some Webserver/Brower combinations appear to do nothing, leaving you with a blank page. Doing a quick local test is a good way to eliminate the bad-syntax problem ---if not 127.0.0.1 = to-tuple system/options/cgi/remote-addr You probably don't want to be sending alert messages from you local testing system. ---set-net [--from email address-- --default-server--] Set to your sending-email address and default mail server ---send to-email "--to-email address--" Replace with the email address of whoever will be dealing with the problem.