aosabook/500lines · error · ServerException

Unknown object '{0}'

Error message

Unknown object '{0}'

What it means

Raised by case_always_fail.act in the 03-handlers server-index-page server. case_always_fail.test returns True unconditionally and is the last entry in Cases, so it catches any request that no earlier case matched (not a file, and not a directory containing an index.html via case_directory_index_file).

Source

Thrown at web-server/code/03-handlers/server-index-page.py:55

        return os.path.join(handler.full_path, 'index.html')

    def test(self, handler):
        return os.path.isdir(handler.full_path) and \
               os.path.isfile(self.index_path(handler))

    def act(self, handler):
        handler.handle_file(self.index_path(handler))

#-------------------------------------------------------------------------------

class case_always_fail(object):
    '''Base case if nothing else worked.'''

    def test(self, handler):
        return True

    def act(self, handler):
        raise ServerException("Unknown object '{0}'".format(handler.path))

#-------------------------------------------------------------------------------

class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    '''
    If the requested path maps to a file, that file is served.
    If anything goes wrong, an error page is constructed.
    '''

    Cases = [case_no_file(),
             case_existing_file(),
             case_directory_index_file(),
             case_always_fail()]

    # How to display an error.
    Error_Page = """\
        <html>
        <body>

View on GitHub (pinned to fba689d101)

Solutions

  1. Add an index.html to the requested directory so case_directory_index_file handles it.
  2. Insert a new case class earlier in the Cases list to handle the object type (e.g. directory listing).
  3. Ensure the requested path is a regular file so case_existing_file matches first.

Example fix

// before
# GET /docs/ with no index.html -> case_always_fail
// after
# add docs/index.html, or add a case_directory_no_index_file handler
Defensive patterns

Strategy: fallback

Validate before calling

# add a case for directories without index BEFORE the always-fail case
class case_directory_no_index_file(object):
    def test(self, handler):
        return os.path.isdir(handler.full_path)
    def act(self, handler):
        handler.list_dir(handler.full_path)

# insert it in Cases before case_always_fail

Try / catch

try:
    for case in self.Cases:
        if case.test(self):
            case.act(self); break
except ServerException:
    self.handle_error(msg)

Prevention

When it happens

Trigger: A request whose full_path exists but matches none of the earlier cases: e.g. a directory with no index.html (case_directory_index_file.test fails), or a special file. The always-fail case then raises ServerException("Unknown object").

Common situations: Requesting a directory that lacks an index.html; pointing at a non-file/non-indexable object; an object type for which no case handler is registered.

Related errors


AI-assisted analysis of aosabook/500lines@fba689d101 (2026-08-13). Data as JSON: /api/errors/d8e7bf620f4a718e. Report an issue: GitHub.