Infrastructure Perspective: Handling and Raising WebErrors
What are WebError's?
W |
try: lock tables calculate if bad condition: raise WebError ("bad_condition","Error Message") continue with regular stuff finally: unlock tables
This ensures that the tables are unlocked irrespective of whether an exception occurred or not.
Raising WebError's
T |
Dictionary Lookup
I |
error_messages = { 'no_perms' : ("No Permissions","You have no permissions for this page"), 'no_crse' : ("No course", "There is no course with specified course number") }
Then when raising the exception you do a
raise WebError ("no_perms{}")
The "{}" at the end of the error string, instructs the handler to do a dictionary lookup and call generate_error_page with the two strings defined in the dictionary. The second argument is ignored in this case. If the specified dictionary entry does not exist, it generates a default error message to the user as well as an error message to the debug.log file.
Function Call based exception handling
A |
You gave me -100 for a course number
Another possible situation: An error has occurred. But some special cases does not require user intervention, the script should be able to take care of it.
In either of these situations you need a function call based exception handling. In this case the exception is raised using
raise WebError ("bad_crse_num()",self.args)
When the exception handler sees a "()" at the end of the first argument, it assumes that you want to handle the exception in your own function. The name of the function is handle_bad_crse_num(self,page,message), i.e. function name is got by prepending the first argument with "handle_". The function should take three arguments: self (since it is a method of the Display class), page and message. The page argument contains the page object which contains the error page. The message argument is whatever was passed in the second argument in your raise statement. In this case it is self.args. Since arguments in python can be any type, you can send as much information as you want to your exception handler, by properly packing it and unpacking it.