Dictionary Lookup

I
f you want to raise the same error messages at many places in your code, or if you want to collect all the error messages together to make your code look cleaner, then you want to use the dictionary based error handling. All you need to do is to define a dictionary called error_messages as a static member of your display class. This dictionary should map short strings to pairs of strings.


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.