Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(web): add missing Flask error handlers for 401, 413, and WebAPIException (#3223)

LearningCircuite25db5db4324
diff --git a/src/local_deep_research/web/app_factory.py b/src/local_deep_research/web/app_factory.py
index 7b3b568264..8b6d362f9c 100644
--- a/src/local_deep_research/web/app_factory.py
+++ b/src/local_deep_research/web/app_factory.py
@@ -842,6 +842,37 @@ def register_error_handlers(app):
return make_response(jsonify({"error": "Server error"}), 500)
return make_response("Server error", 500)
+ @app.errorhandler(401)
+ def handle_unauthorized(error):
+ if request.path.startswith("/api/") or request.path.startswith(
+ "/settings/api/"
+ ):
+ return make_response(
+ jsonify({"error": "Authentication required"}),
+ 401,
+ )
+ from .auth.decorators import _safe_redirect_to_login
+
+ return _safe_redirect_to_login()
+
+ @app.errorhandler(413)
+ def handle_request_too_large(error):
+ if request.path.startswith("/api/"):
+ return make_response(
+ jsonify({"error": "Request too large"}),
+ 413,
+ )
+ return make_response("Request too large", 413)
+
+ # Import locally to avoid circular imports
+ from .exceptions import WebAPIException
+
+ @app.errorhandler(WebAPIException)
+ def handle_web_api_exception(error):
+ """Handle WebAPIException and return JSON."""
+ logger.exception(f"Web API error: {error.message}")
+ return jsonify(error.to_dict()), error.status_code
+
# Handle CSRF validation errors with helpful message
try:
from flask_wtf.csrf import CSRFError
diff --git a/tests/web/test_error_handler_behavior.py b/tests/web/test_error_handler_behavior.py
index a20e433ece..a051d656fb 100644
--- a/tests/web/test_error_handler_behavior.py
+++ b/tests/web/test_error_handler_behavior.py
@@ -2,7 +2,10 @@
Tests for error handler *behavior* in app_factory.register_error_handlers().
Existing tests cover registration and 404 content; these test:
+- 401 handler JSON vs redirect branching
+- 413 handler JSON vs text branching
- 500 handler JSON vs text branching
+- WebAPIException handler JSON conversion
- CSRF handler IP-based messaging (private/public/proxied/HTTPS)
- NewsAPIException handler JSON conversion
- _is_private_ip edge cases (IPv4-mapped IPv6, link-local, multicast)
@@ -11,6 +14,8 @@ Existing tests cover registration and 404 content; these test:
import pytest
from unittest.mock import patch
+from flask import abort
+
@pytest.fixture
def app():
@@ -21,11 +26,33 @@ def app():
app, _ = create_app()
app.config["TESTING"] = True
app.config["WTF_CSRF_ENABLED"] = False
- # Must disable exception propagation so 500 handler is invoked
+ # Must disable exception propagation so error handlers are invoked
app.config["PROPAGATE_EXCEPTIONS"] = False
app.config["TRAP_HTTP_EXCEPTIONS"] = False
- # Add routes that trigger 500 errors
+ # Routes that trigger 401 errors
+ @app.route("/api/trigger-401")
+ def api_trigger_401():
+ abort(401)
+
+ @app.route("/settings/api/trigger-401")
+ def settings_api_trigger_401():
+ abort(401)
+
+ @app.route("/trigger-401")
+ def web_trigger_401():
+ abort(401)
+
+ # Routes that trigger 413 errors
+ @app.route("/api/trigger-413")
+ def api_trigger_413():
+ abort(413)
+
+ @app.route("/trigger-413")
+ def web_trigger_413():
+ abort(413)
+
+ # Routes that trigger 500 errors
@app.route("/api/trigger-500")
def api_trigger_500():
raise Exception("boom")
@@ -34,10 +61,35 @@ def app():
def web_trigger_500():
raise Exception("boom")
- # Add route that raises NewsAPIException
+ # Route that raises WebAPIException
+ @app.route("/api/trigger-web-api-error")
+ def trigger_web_api_error():
+ from local_deep_research.web.exceptions import (
+ WebAPIException,
+ )
+
+ raise WebAPIException(
+ message="Something went wrong",
+ status_code=400,
+ error_code="BAD_REQUEST",
+ details={"field": "name"},
+ )
+
+ # Route that raises AuthenticationRequiredError
+ @app.route("/api/trigger-auth-error")
+ def trigger_auth_error():
+ from local_deep_research.web.exceptions import (
+ AuthenticationRequiredError,
+ )
+
+ raise AuthenticationRequiredError()
+
+ # Route that raises NewsAPIException
@app.route("/api/trigger-news-error")
def trigger_news_error():
- from local_deep_research.news.exceptions import NewsAPIException
+ from local_deep_research.news.exceptions import (
+ NewsAPIException,
+ )
raise NewsAPIException(
message="Feed unavailable",
@@ -53,6 +105,50 @@ def client(app):
return app.test_client()
+class TestUnauthorizedHandler:
+ """Tests for the 401 error handler behavior."""
+
+ def test_401_returns_json_for_api_path(self, client):
+ """API paths get JSON error response for 401."""
+ response = client.get("/api/trigger-401")
+ assert response.status_code == 401
+ data = response.get_json()
+ assert data is not None
+ assert data["error"] == "Authentication required"
+
+ def test_401_returns_json_for_settings_api_path(self, client):
+ """/settings/api/ paths get JSON error response for 401."""
+ response = client.get("/settings/api/trigger-401")
+ assert response.status_code == 401
+ data = response.get_json()
+ assert data is not None
+ assert data["error"] == "Authentication required"
+
+ def test_401_redirects_for_web_path(self, client):
+ """Non-API paths redirect to login page for 401."""
+ response = client.get("/trigger-401")
+ assert response.status_code == 302
+ assert "/auth/login" in response.location
+
+
+class TestRequestTooLargeHandler:
+ """Tests for the 413 error handler behavior."""
+
+ def test_413_returns_json_for_api_path(self, client):
+ """API paths get JSON error response for 413."""
+ response = client.get("/api/trigger-413")
+ assert response.status_code == 413
+ data = response.get_json()
+ assert data is not None
+ assert data["error"] == "Request too large"
+
+ def test_413_returns_text_for_web_path(self, client):
+ """Non-API paths get plain text error response for 413."""
+ response = client.get("/trigger-413")
+ assert response.status_code == 413
+ assert response.data == b"Request too large"
+
+
class TestServerErrorHandler:
"""Tests for the 500 error handler behavior."""
@@ -165,6 +261,36 @@ class TestNewsAPIExceptionHandler:
assert data["status_code"] == 503
+class TestWebAPIExceptionHandler:
+ """Tests for WebAPIException error handler."""
+
+ def test_web_api_exception_returns_json(self, client):
+ """WebAPIException is converted to JSON with to_dict() fields."""
+ response = client.get("/api/trigger-web-api-error")
+ assert response.status_code == 400
+ data = response.get_json()
+ assert data is not None
+ assert data["status"] == "error"
+ assert data["message"] == "Something went wrong"
+ assert data["error_code"] == "BAD_REQUEST"
+ assert data["details"] == {"field": "name"}
+
+ def test_web_api_exception_preserves_status_code(self, client):
+ """Response uses the exception's status_code, not a default."""
+ response = client.get("/api/trigger-web-api-error")
+ assert response.status_code == 400
+
+ def test_authentication_required_error_returns_401(self, client):
+ """AuthenticationRequiredError returns 401 with structured JSON."""
+ response = client.get("/api/trigger-auth-error")
+ assert response.status_code == 401
+ data = response.get_json()
+ assert data is not None
+ assert data["status"] == "error"
+ assert "Authentication required" in data["message"]
+ assert data["error_code"] == "AUTHENTICATION_REQUIRED"
+
+
class TestIsPrivateIpEdgeCases:
"""Edge cases for _is_private_ip not covered by existing tests."""