Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Function forward declaration?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-04-07 (18:04)
From: Benjamin Geer <ben@s...>
Subject: Re: [Caml-list] Function forward declaration?
Richard Jones wrote:
> This is the sort of thing which I'd like to write.

It looks as if using exceptions would simplify your example.  If your 
check_permissions and find_resource functions threw exceptions 
containing error messages (e.g. Failure), you could write:

let run dbh q userid =
     check_permissions userid;
     let query = q#param "query" in
       if is_empty query then
         failwith "Please enter a query"
         let resource =
             find_resource dbh query
           with Not_found ->
             failwith "That resource doesn't exist in the database"
           (* do some work *);
           StdPages.ok q "OK, your query was executed"
   with Failure msg ->
     StdPages.error q msg ;;

Besides being more concise, I think this is an improvement because it 
separates the identification of an error with the way the error is 
reported.  This means that if someday you want to change the way you 
handle errors, you have less code to change; in the version above, there 
is only one line of code that knows about StdPages.error.

(It would perhaps be even better to wrap the calls to StdPages.ok and 
StdPages.error in more generic report_success and report_error 
functions, so that if someday you changed your mind about having a 
single standard error page, or if you decided to implement a completely 
different sort of GUI, e.g. using GTK+, you wouldn't have to change the 
'run' function at all.)


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: