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-status-code server as the final else branch: full_path exists (os.path.exists True) but is not a regular file (os.path.isfile False). In this variant nothing else is handled, so directories, special files, and sockets all fall through to this ServerException, which the surrounding except routes to handle_error.

Source

Thrown at web-server/code/02-serve-static/server-status-code.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, 404)

View on GitHub (pinned to fba689d101)

Solutions

  1. Request a concrete file rather than a directory, or add directory/index handling (as the 03-handlers variants do).
  2. Ensure the served tree contains only regular files for this minimal variant.
  3. Add a new branch/case for the object type before the else so it is handled instead of rejected.

Example fix

// before
# request http://host/images/ (a directory) -> Unknown object
// after
# request http://host/images/logo.png (a regular file)
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):
    # e.g. a directory: handle explicitly instead of reaching 'Unknown object'
    if os.path.isdir(full_path):
        self.handle_dir(full_path)

Try / catch

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

Prevention

When it happens

Trigger: A GET request whose path exists but is not a regular file, e.g. requesting a directory (no directory/index handling exists in 02-serve-static), a device node, or a broken-but-present symlink.

Common situations: Requesting a directory URL when the server only serves files; serving from a path containing special files; pointing at a symlink that os.path.isfile rejects.

Related errors


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