Script Library: 1213 scripts
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Documentation for: cgi-debug.r


cgi-debug function

Sunanda
November 2003

Contents

1. 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

2. 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
 ]

3. 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

4. Notes

4.1 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
  ]

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/script

Is the name of the script being executed.

4.4 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

4.5 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.

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.