Middleware API

Error notification middleware for Django.

class django_growl.middleware.GrowlErrorMiddleware(get_response)[source]

Bases: object

Middleware to send error notifications to Growl

__init__(get_response)[source]
process_exception(request, exception)[source]

GrowlErrorMiddleware

class django_growl.middleware.GrowlErrorMiddleware(get_response)[source]

Middleware to send error notifications to Growl

Middleware to send Growl notifications when Django encounters errors.

This middleware catches exceptions and sends detailed error notifications including the exception message, stacktrace, request path, and HTTP method.

Configuration:

Add to MIDDLEWARE in settings:

MIDDLEWARE = [
    # ... other middleware
    'django_growl.middleware.GrowlErrorMiddleware',
]

Settings:

# Enable error notifications (default: True)
GROWL_NOTIFY_ERRORS = True

# Make error notifications sticky (default: True)
GROWL_STICKY_ERRORS = True

Attributes:

notify_errors

Whether error notifications are enabled.

Type:

bool

sticky_errors

Whether error notifications should be sticky.

Type:

bool

__init__(get_response)[source]
__call__(request)[source]

Call self as a function.

process_exception(request, exception)[source]

Methods

GrowlErrorMiddleware.__init__(get_response)[source]

Initialize the middleware.

Parameters:

get_response (Callable) – Django’s get_response callable

Reads configuration from Django settings:

  • GROWL_NOTIFY_ERRORS - Enable/disable error notifications

  • GROWL_STICKY_ERRORS - Make error notifications sticky

GrowlErrorMiddleware.__call__(request)[source]

Call self as a function.

Process the request through middleware chain.

Parameters:

request (django.http.HttpRequest) – Django HTTP request object

Returns:

Response from next middleware/view

Return type:

django.http.HttpResponse

process_exception(request, exception)[source]

Handle exceptions and send notifications.

Parameters:
Returns:

None (lets Django handle the exception normally)

Return type:

None

This method is called automatically by Django when an exception occurs. It formats the error information and sends a Growl notification with:

  • Exception type and message

  • Request path and method

  • Full stacktrace (truncated if too long)

Example notification format:

Title: Django Error: ValueError

Message:
Error: invalid literal for int()
Path: /api/users/abc/
Method: GET

Traceback:
File "views.py", line 42, in user_detail
  user_id = int(user_id_str)
ValueError: invalid literal for int()

Usage Example

Once configured, the middleware automatically sends notifications for all unhandled exceptions:

# views.py
def my_view(request):
    # This error will trigger a Growl notification
    raise ValueError("Something went wrong")

# The notification will include:
# - Error type: ValueError
# - Error message: Something went wrong
# - Request path: /my-view/
# - Request method: GET
# - Full stacktrace

Disabling Error Notifications

Temporarily disable error notifications:

# settings.py
GROWL_NOTIFY_ERRORS = False

Or remove the middleware:

MIDDLEWARE = [
    # ... other middleware
    # 'django_growl.middleware.GrowlErrorMiddleware',  # Commented out
]