{"record":{"id":"ca3266343f741d9d","repo":"aosabook/500lines","slug":"unknown-object-0-ca3266","errorCode":null,"errorMessage":"Unknown object '{0}'","messagePattern":"Unknown object '(.+?)'","errorType":"http","errorClass":"ServerException","httpStatus":404,"severity":"error","filePath":"web-server/code/05-refactored/server.py","lineNumber":128,"sourceCode":"            handler.handle_error(msg)\n\n    def test(self, handler):\n        return os.path.isdir(handler.full_path) and \\\n               not os.path.isfile(self.index_path(handler))\n\n    def act(self, handler):\n        self.list_dir(handler, handler.full_path)\n\n#-------------------------------------------------------------------------------\n\nclass case_always_fail(base_case):\n    '''Base case if nothing else worked.'''\n\n    def test(self, handler):\n        return True\n\n    def act(self, handler):\n        raise ServerException(\"Unknown object '{0}'\".format(handler.path))\n\n#-------------------------------------------------------------------------------\n\nclass RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):\n    '''\n    If the requested path maps to a file, that file is served.\n    If anything goes wrong, an error page is constructed.\n    '''\n\n    Cases = [case_no_file(),\n             case_cgi_file(),\n             case_existing_file(),\n             case_directory_index_file(),\n             case_directory_no_index_file(),\n             case_always_fail()]\n\n    # How to display an error.\n    Error_Page = \"\"\"\\","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/aosabook/500lines/blob/fba689d101eb5600f5c8f4d7fd79912498e950e2/web-server/code/05-refactored/server.py#L110-L146","documentation":"Thrown by case_always_fail.act, the terminal handler in the case dispatch chain. case_always_fail.test unconditionally returns True, so its act runs only when every earlier case (case_no_file, case_cgi_file, case_existing_file, and the directory cases) failed to match. It signals that the path exists but is none of the supported object types, raising ServerException(\"Unknown object '{path}'\").","triggerScenarios":"handler.full_path exists but is not a regular file, not a CGI script, and not a servable directory (e.g. no index file and directory listing disabled/failed). Concretely: a socket, FIFO, device node, or a directory whose index case was omitted from the Cases list.","commonSituations":"A new filesystem object type was added but no case class was registered for it; the Cases list was reordered so a directory case is missing; requesting a special file like /dev/null through the server; permission issues that make stat behave oddly.","solutions":["Inspect the object type of handler.full_path with os.path.isfile / os.path.isdir / os.path.islink to see which case should have matched.","Ensure the Cases list includes handlers for every object type you serve (directory index, directory listing, CGI).","Add a dedicated case before case_always_fail for the unhandled object type.","If the object should not be served, treat this as expected behaviour and let the error page render."],"exampleFix":"# before: Cases missing a directory-index case, so a real dir hits always_fail\nCases = [case_no_file(), case_cgi_file(), case_existing_file(), case_always_fail()]\n\n# after: insert the directory cases before the catch-all\nCases = [case_no_file(),\n         case_cgi_file(),\n         case_existing_file(),\n         case_directory_index_file(),\n         case_directory_no_index_file(),\n         case_always_fail()]","handlingStrategy":"validation","validationCode":"# classify the object before relying on the case chain\nimport os\np = handler.full_path\nkind = ('dir' if os.path.isdir(p) else\n        'file' if os.path.isfile(p) else\n        'special')\nif kind == 'special':\n    # add a case or reject explicitly before case_always_fail\n    return None","typeGuard":null,"tryCatchPattern":"try:\n    for case in Cases:\n        if case.test(handler):\n            case.act(handler)\n            break\nexcept ServerException:\n    # render the generic error page\n    handler.error_page()","preventionTips":["Order Cases from most specific to least, always ending with case_always_fail.","Register a case class for every filesystem object type you intend to serve.","Reject special files (sockets, devices) explicitly with an early case.","Unit-test the chain against a file, a directory with/without index, and a missing path."],"tags":[],"backgroundTag":null,"analyzedSha":"fba689d101eb5600f5c8f4d7fd79912498e950e2","analyzedAt":"2026-08-13T06:26:32.792Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}