aosabook/500lines · error · ServerException

Unknown object '{0}'

Error message

Unknown object '{0}'

What it means

Raised in do_GET of the 02-serve-static server (plain server.py) in the final else branch: the path exists but is not a regular file (os.path.isfile False). Because this variant only handles regular files, any existing non-file object (directory, special file) becomes 'Unknown object'.

Source

Thrown at web-server/code/02-serve-static/server.py:44

    # Classify and handle request.
    def do_GET(self):
        try:

            # Figure out what exactly is being requested.
            full_path = os.getcwd() + self.path

            # It doesn't exist...
            if not os.path.exists(full_path):
                raise ServerException("'{0}' not found".format(self.path))

            # ...it's a file...
            elif os.path.isfile(full_path):
                self.handle_file(full_path)

            # ...it's something we don't handle.
            else:
                raise ServerException("Unknown object '{0}'".format(self.path))

        # Handle errors.
        except Exception as msg:
            self.handle_error(msg)

    def handle_file(self, full_path):
        try:
            with open(full_path, 'rb') as reader:
                content = reader.read()
            self.send_content(content)
        except IOError as msg:
            msg = "'{0}' cannot be read: {1}".format(self.path, msg)
            self.handle_error(msg)

    # Handle unknown objects.
    def handle_error(self, msg):
        content = self.Error_Page.format(path=self.path, msg=msg)
        self.send_content(content)

View on GitHub (pinned to fba689d101)

Solutions

  1. Request a regular file instead of a directory.
  2. Add directory or index handling (upgrade to a 03-handlers variant).
  3. Ensure only regular files live in the served root for this minimal server.

Example fix

// before
# GET /data/ (directory) -> Unknown object
// after
# register a case_directory handler, or request /data/file.txt
Defensive patterns

Strategy: fallback

Validate before calling

import os
full_path = os.getcwd() + self.path
if os.path.exists(full_path) and not os.path.isfile(full_path):
    if os.path.isdir(full_path):
        self.handle_dir(full_path)   # handle instead of rejecting

Try / catch

try:
    # dispatch
except ServerException:
    self.handle_error(msg)

Prevention

When it happens

Trigger: A GET request for a path that exists but is not os.path.isfile, e.g. a directory, a FIFO, a device file, or a broken symlink that still reports exists=True.

Common situations: Requesting a directory URL; serving a tree containing special files; the always-fallthrough because no directory handler is registered.

Related errors


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