{"id":10365,"library":"zexceptions","title":"zExceptions","description":"zExceptions is a Python library containing common exceptions used primarily within the Zope and Plone web frameworks. It provides specialized exceptions like `NotFound`, `Redirect`, `Unauthorized`, and `Forbidden` that map to HTTP status codes, simplifying error handling in Zope applications. The current version is 6.0, and it follows an infrequent release cadence, often aligning with Zope framework updates.","status":"active","version":"6.0","language":"en","source_language":"en","source_url":"https://github.com/zopefoundation/zExceptions","tags":["zope","exceptions","web","plone"],"install":[{"cmd":"pip install zexceptions","lang":"bash","label":"Default Install"}],"dependencies":[],"imports":[{"symbol":"NotFound","correct":"from zexceptions import NotFound"},{"symbol":"Redirect","correct":"from zexceptions import Redirect"},{"symbol":"Unauthorized","correct":"from zexceptions import Unauthorized"},{"symbol":"Forbidden","correct":"from zexceptions import Forbidden"}],"quickstart":{"code":"from zexceptions import NotFound, Unauthorized, Redirect\n\ndef get_resource(resource_id: str, is_authenticated: bool):\n    if resource_id == \"non_existent\":\n        raise NotFound(\"The requested resource was not found.\")\n    elif resource_id == \"private\" and not is_authenticated:\n        raise Unauthorized(\"Authentication required to access this resource.\")\n    elif resource_id == \"old_path\":\n        # Redirect to a new path with a 301 status code\n        raise Redirect(location=\"/new_path_for_old_resource\", code=301)\n    return f\"Resource {resource_id} data.\"\n\n# Example Usage:\ntry:\n    print(get_resource(\"some_item\", True))\n    print(get_resource(\"private\", False))\nexcept NotFound as e:\n    print(f\"Caught NotFound: {e}\")\nexcept Unauthorized as e:\n    print(f\"Caught Unauthorized: {e}\")\nexcept Redirect as e:\n    print(f\"Caught Redirect: {e.location} with code {e.code}\")\n\ntry:\n    get_resource(\"non_existent\", True)\nexcept NotFound as e:\n    print(f\"Caught NotFound (again): {e}\")","lang":"python","description":"Demonstrates raising and catching common `zexceptions` like `NotFound`, `Unauthorized`, and `Redirect`, typical in a web request handling scenario. `Redirect` takes a `location` and an optional `code` for the HTTP redirect status."},"warnings":[{"fix":"Upgrade your Python environment to 3.10 or newer. If you must use an older Python version, pin `zexceptions` to `<6.0`.","message":"zexceptions version 6.0 requires Python 3.10 or newer.","severity":"breaking","affected_versions":"6.0+"},{"fix":"If upgrading from `zexceptions < 5.0` to `5.0` or later, code relying on `HTTPExceptionHandler` will break. Remove or refactor code that uses `HTTPExceptionHandler`. Modern Zope/Plone versions handle exceptions directly or through WSGI middleware, making this class obsolete.","message":"The `HTTPExceptionHandler` class was removed in `zexceptions` 5.0.","severity":"breaking","affected_versions":"5.0+"},{"fix":"While you can import and raise zexceptions in any Python application, their specific handling (e.g., mapping to HTTP responses) is usually managed by the Zope or Plone framework. Without such a framework, you'll need to implement custom exception handling to convert them into appropriate HTTP responses (e.g., in Flask/Django middleware).","message":"Using zexceptions outside of a Zope or Plone framework context.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Install the library using pip: `pip install zexceptions`","cause":"The `zexceptions` library is not installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'zexceptions'"},{"fix":"Remove or refactor code that imports `HTTPExceptionHandler`. This class is no longer part of the library. Consult Zope/Plone documentation for modern exception handling.","cause":"The `HTTPExceptionHandler` class was removed in `zexceptions` version 5.0. Your code is attempting to import a deprecated component after upgrading to `zexceptions` 5.0 or newer.","error":"ImportError: cannot import name 'HTTPExceptionHandler' from 'zexceptions' (path/to/site-packages/zexceptions/__init__.py)"},{"fix":"Ensure `zexceptions` are raised using `raise NotFound(...)` and then caught in an `except NotFound:` block. Do not attempt to iterate over them or call methods not related to standard exception handling.","cause":"Misusing a `zexceptions` instance (e.g., `NotFound()`) as if it were a data structure or iterable, instead of raising it as an exception.","error":"TypeError: 'NotFound' object is not iterable"}]}