Documention for: cgi-debug.r
Created by: sunanda
on: 24-Nov-2003
Last updated by: sunanda on: 26-Nov-2003
Format: html
Downloaded on: 7-Dec-2024

cgi-debug function

Sunanda
November 2003

Contents

1. Purpose
2. Usage
3. Example
4. Notes
4.1 Silent refinement
4.2 if error? cgi-debug-capture-error: try [do target-code 1]
4.3 system/options/script
4.4 ip-address: none
4.5 if not 127.0.0.1 = to-tuple system/options/cgi/remote-addr
4.6 set-net [--from email address-- --default-server--]
4.7 send to-email "--to-email address--"

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:

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:

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.