Showing possible software problems with a warning

The idea of warnings aimed at end users is a bit nebulous; did the application work or did it fail? What does a warning really mean? Is there something the user should do differently?

Because of this potential ambiguity, warnings in the user interface aren't a great idea. To be truly usable, a program should either work correctly or should not work at all. When there's an error, the error message should include advice for the user's response to the problem. We shouldn't impose a burden on the user to judge the quality of the output and determine its fitness for purpose. We'll elaborate on this point.

A program should either work correctly or it should not work at all.

One potential unambiguous use for end user warnings is to alert the user that the output is incomplete. An application may have a problem completing a network connection, for example; the essential results are correct, but one of the data sources didn't work properly.

There are situations where an application takes an action that is not what the user requested, and the output is valid and useful. In the case of a network problem, a default behavior can be used in spite of the network problem. Generally, replacing something faulty with something correct but not exactly what the user requested is a good candidate for a warning. This kind of warning is best done with logging at the WARN level, not with the warnings module. The warnings module produces one-time messages; we may want to provide more details to the user. Here's how we might use a simple Logger.warn() message to describe the problem in the log:

try: 
    with urllib.request.urlopen("http://host/resource/", timeout=30) as resource: 
        content = json.load(resource) 
except socket.timeout as e: 
    self.log.warn(
"Missing information from http://host/resource") content= []

If a timeout occurs, a warning message is written to the log and the program keeps running. The content of the resource will be set to an empty list. The log message will be written every time. A warnings module warning is ordinarily shown only once from a given location in the program and is suppressed after that.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset