This site contains older material on Eiffel. For the main Eiffel page, see http://www.eiffel.com.

5. USING THE DEBUGGING FACILITIES

One of the most difficult steps in writing form processing software with traditional CGI scripts is debugging. When the script does not complete successfully, it exits with an error code; in such a case the Web server merely reports an internal error occurred, which is not much help.

EiffelWeb makes the debugging process much more convenient through features of class CGI_INTERFACE.

Using make_debug as a creation procedure

Besides make, CGI_INTERFACE provides another procedure that can be used as creation procedure: make_debug, which extends make. Using make_debug you can perform extra operations, for example set environment variables to special values, and you can parse the input before starting any processing.

A piece of methodological advice: once you are done with debugging, remember to set the creation procedure back to the normal make, just in case make_debug sets environment variables to values which are not appropriate for normal processing, or generates output that is intended for you and not for your visitors.

Environment variables

To recreate the context in which the application is called by the WWW server, you need to set the corresponding environment variables. This can lead to one of the most annoying aspects of traditional CGI script creation and debugging. You must have exit the working environment, set the variables, and restart the environment; or you could set the variables from the script, but this is just as inconvenient.

EiffelWeb provides a better solution: set the variables on-the-fly by passing them as argument to the script. You will for example call the application as:

    your_script REQUEST_METHOD=GET QUERY_STRING="name=foo"

setting the variables REQUEST_METHOD and QUERY_STRING to GET and to name=foo.

To set the default values, make_debug calls the procedure set_environment; in its version inherited from CGI_INTERFACE, set_environment does nothing, but you can redefine it to set environment variables to specific values. To set an individual environment variable, use set_environment_variable. The defaults will be overriden by the scripts arguments if any.

Here is an example class using these facilities:

class
CGI_SCRIPT
inherit
CGI_INTERFACE
redefine
set_environment
end
creation
make_debug
feature

set_environment is
-- Set two environment variables
do
set_environment_variable ("SCRIPT_NAME", "form_handler");
set_environment_variable ("SERVER_NAME", "test.oursite.com");

end
end -- class CGI_SCRIPT

Of course, you can still test your script manually and set the environment variables from the command line as follows (here expressed in C-shell syntax):

    setenv REQUEST_METHOD "GET"
    setenv QUERY_STRING ""
    setenv CONTENT_LENGTH 0
    setenv SERVER_NAME "no_server"
    setenv SCRIPT_NAME "test_script"

Error handling

The creation procedure, whether make or make_debug, will catch any exception occurring during the execution of the script and will handle it according to the choosen option which set_environment can set by calling one of the three following procedures:

  • set_no_debug: an exception will terminate processing with no further action.
  • set_message: an exception will terminate processing and display a message explaining that an internal error occurred.
  • set_exception_trace: an exception will cause the the script to terminate and display the exception trace. This will only be in effect if the Lace option exception_trace is on; this is the case by default in all modes except final mode, for which you may turn it on through the line exception_trace (yes) in the default paragraph of your Ace.

In all cases the application will not return an error code but keep control of the execution, therefore preventing the Web server from displaying its own message.

If you detect an irrecoverable error during processing, you can stop execution using procedure raise_error. This will display an error message, which you can set through an earlier call to set_error.

All the features listed in this section belong to class CGI_INTERFACE, whose complete specification appears in Appendix A.

An example using make_debug

Let's use again the basic example. Employing make_debug instead of make as creation procedure will allow us to change the debug level - e.g. to get the exception trace if an exception occurs. This will also allow us to set default values for some environment variables. This time instead of an indented list the results will be displayed in a bulleted list.

class
CGI_DEMO
inherit
CGI_INTERFACE
redefine
set_environment
end
creation
make_debug
feature

set_environment is
do
set_exception_trace;
set_environment_variable ("SERVER_NAME", "no_server");
set_environment_variable ("SCRIPT_NAME", "test_script");

end

execute is
local
i: INTEGER;
vl: LINKED_LIST[STRING]

do
generate_html_header;
put_basic ("You have submitted the following name / value pairs:");
put_unordered_list_start;
from
i := fields.lower
until
i > fields.upper
loop
put_list_item_start;
put_bold (fields.item (i));
put_line_break;
vl := value_list (fields.item (i));

from
vl.start
until
vl.after
loop
put_basic (vl.item);
vl.forth;

if not vl.after then
io.putstring (", ")
end
end;
put_list_item_end;
i := i + 1

end;
put_unordered_list_end
end
end -- class CGI_DEMO

Table of contents | Next chapter