zExceptions

6.0 · active · verified Fri Apr 17

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

from zexceptions import NotFound, Unauthorized, Redirect

def get_resource(resource_id: str, is_authenticated: bool):
    if resource_id == "non_existent":
        raise NotFound("The requested resource was not found.")
    elif resource_id == "private" and not is_authenticated:
        raise Unauthorized("Authentication required to access this resource.")
    elif resource_id == "old_path":
        # Redirect to a new path with a 301 status code
        raise Redirect(location="/new_path_for_old_resource", code=301)
    return f"Resource {resource_id} data."

# Example Usage:
try:
    print(get_resource("some_item", True))
    print(get_resource("private", False))
except NotFound as e:
    print(f"Caught NotFound: {e}")
except Unauthorized as e:
    print(f"Caught Unauthorized: {e}")
except Redirect as e:
    print(f"Caught Redirect: {e.location} with code {e.code}")

try:
    get_resource("non_existent", True)
except NotFound as e:
    print(f"Caught NotFound (again): {e}")

view raw JSON →