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.

refactor: replace bare except clauses with specific exception types (#1983)

LearningCircuita1c3ae70593c
diff --git a/src/local_deep_research/advanced_search_system/candidate_exploration/adaptive_explorer.py b/src/local_deep_research/advanced_search_system/candidate_exploration/adaptive_explorer.py
index e05348cfd6..e90a026508 100644
--- a/src/local_deep_research/advanced_search_system/candidate_exploration/adaptive_explorer.py
+++ b/src/local_deep_research/advanced_search_system/candidate_exploration/adaptive_explorer.py
@@ -252,7 +252,10 @@ Query:
try:
response = self.model.invoke(prompt).content.strip()
return response if response != base_query else None
- except Exception:
+ except Exception as e:
+ logger.debug(
+ f"Error generating synonym query for '{base_query}': {e}"
+ )
return None
def _category_exploration_query(
@@ -278,7 +281,10 @@ Related search term:
try:
response = self.model.invoke(prompt).content.strip()
return response if response != base_query else None
- except Exception:
+ except Exception as e:
+ logger.debug(
+ f"Error generating related terms query for '{base_query}': {e}"
+ )
return None
def _constraint_focused_query(
diff --git a/src/local_deep_research/advanced_search_system/constraint_checking/evidence_analyzer.py b/src/local_deep_research/advanced_search_system/constraint_checking/evidence_analyzer.py
index 646a71e739..34c70c998e 100644
--- a/src/local_deep_research/advanced_search_system/constraint_checking/evidence_analyzer.py
+++ b/src/local_deep_research/advanced_search_system/constraint_checking/evidence_analyzer.py
@@ -114,7 +114,7 @@ UNCERTAINTY: [score]
if match:
try:
return float(match.group(1))
- except ValueError:
+ except (ValueError, TypeError):
pass
return 0.1 # Default low score
diff --git a/src/local_deep_research/advanced_search_system/strategies/dual_confidence_strategy.py b/src/local_deep_research/advanced_search_system/strategies/dual_confidence_strategy.py
index ac22f4d4b0..2add898f88 100644
--- a/src/local_deep_research/advanced_search_system/strategies/dual_confidence_strategy.py
+++ b/src/local_deep_research/advanced_search_system/strategies/dual_confidence_strategy.py
@@ -159,7 +159,7 @@ UNCERTAINTY: [score]
if match:
try:
return float(match.group(1))
- except ValueError:
+ except (ValueError, TypeError):
pass
return 0.1 # Default low score
diff --git a/src/local_deep_research/advanced_search_system/strategies/evidence_based_strategy_v2.py b/src/local_deep_research/advanced_search_system/strategies/evidence_based_strategy_v2.py
index 0b64d4a457..d6b2bed093 100644
--- a/src/local_deep_research/advanced_search_system/strategies/evidence_based_strategy_v2.py
+++ b/src/local_deep_research/advanced_search_system/strategies/evidence_based_strategy_v2.py
@@ -14,6 +14,7 @@ from datetime import datetime, UTC
from typing import Any, Dict, List, Optional, Set, Tuple
from langchain_core.language_models import BaseChatModel
+from loguru import logger
from ...utilities.search_utilities import remove_think_tags
from ..candidates.base_candidate import Candidate
@@ -930,8 +931,9 @@ class EnhancedEvidenceBasedStrategy(EvidenceBasedStrategy):
line.strip() for line in content.split("\n") if line.strip()
]
return synonyms[:3]
- except Exception:
- return [] # Return empty list on error
+ except Exception as e:
+ logger.debug(f"Error getting synonyms for '{term}': {e}")
+ return []
def _find_common_terms(
self, constraint1: Constraint, constraint2: Constraint
diff --git a/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py b/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py
index 8310a4aba7..dcd8e91c6d 100644
--- a/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py
+++ b/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py
@@ -441,7 +441,7 @@ Response:"""
try:
domain = urlparse(url).netloc if url else ""
- except Exception:
+ except (ValueError, AttributeError):
domain = ""
# Format source info without restrictions
diff --git a/src/local_deep_research/api/client.py b/src/local_deep_research/api/client.py
index 9f06ba41ee..e3bbea1715 100644
--- a/src/local_deep_research/api/client.py
+++ b/src/local_deep_research/api/client.py
@@ -230,7 +230,7 @@ class LDRClient:
error_msg = error_data[0].get("message", "Unknown error")
else:
error_msg = str(error_data)
- except Exception:
+ except (ValueError, KeyError, AttributeError):
error_msg = response.text[:200]
raise RuntimeError(f"Failed to start research: {error_msg}")
diff --git a/src/local_deep_research/benchmarks/web_api/benchmark_service.py b/src/local_deep_research/benchmarks/web_api/benchmark_service.py
index 96b8d3697a..36638b8c43 100644
--- a/src/local_deep_research/benchmarks/web_api/benchmark_service.py
+++ b/src/local_deep_research/benchmarks/web_api/benchmark_service.py
@@ -975,8 +975,8 @@ class BenchmarkService:
# Roll back the session on error to prevent PendingRollbackError
try:
session.rollback()
- except:
- pass
+ except Exception:
+ logger.debug("Failed to rollback session after sync error")
return saved_count
diff --git a/src/local_deep_research/database/session_context.py b/src/local_deep_research/database/session_context.py
index a1f9c78fca..135812435c 100644
--- a/src/local_deep_research/database/session_context.py
+++ b/src/local_deep_research/database/session_context.py
@@ -131,7 +131,7 @@ def get_user_db_session(
try:
session.close()
except Exception:
- pass
+ logger.debug("Failed to close session during cleanup")
def with_user_database(func: Callable) -> Callable:
diff --git a/src/local_deep_research/domain_classifier/classifier.py b/src/local_deep_research/domain_classifier/classifier.py
index 49183b0fb7..1824626378 100644
--- a/src/local_deep_research/domain_classifier/classifier.py
+++ b/src/local_deep_research/domain_classifier/classifier.py
@@ -310,7 +310,7 @@ JSON Response:"""
domain = domain[4:]
if domain:
domains.add(domain)
- except Exception:
+ except (ValueError, AttributeError):
continue
results["total"] = len(domains)
diff --git a/src/local_deep_research/metrics/database.py b/src/local_deep_research/metrics/database.py
index a4a550fe6a..ae9d060bb1 100644
--- a/src/local_deep_research/metrics/database.py
+++ b/src/local_deep_research/metrics/database.py
@@ -38,7 +38,7 @@ class MetricsDatabase:
from flask import session as flask_session
user = flask_session.get("username")
- except:
+ except (ImportError, RuntimeError):
pass
if not user:
diff --git a/src/local_deep_research/news/subscription_manager/scheduler.py b/src/local_deep_research/news/subscription_manager/scheduler.py
index 393a004070..7a5cfe4ecd 100644
--- a/src/local_deep_research/news/subscription_manager/scheduler.py
+++ b/src/local_deep_research/news/subscription_manager/scheduler.py
@@ -715,7 +715,7 @@ class NewsScheduler:
completed_at_obj = datetime.fromisoformat(
research.completed_at.replace("Z", "+00:00")
)
- except:
+ except (ValueError, TypeError, AttributeError):
completed_at_obj = None
else:
completed_at_obj = research.completed_at
diff --git a/src/local_deep_research/research_library/downloaders/biorxiv.py b/src/local_deep_research/research_library/downloaders/biorxiv.py
index 6369e16e1b..20ae4ee2ce 100644
--- a/src/local_deep_research/research_library/downloaders/biorxiv.py
+++ b/src/local_deep_research/research_library/downloaders/biorxiv.py
@@ -89,8 +89,7 @@ class BioRxivDownloader(BaseDownloader):
skip_reason="bioRxiv/medRxiv server temporarily unavailable"
)
except requests.RequestException:
- # Network error checking server - proceed with generic error
- pass
+ logger.debug("Failed to check bioRxiv/medRxiv URL: %s", url)
return DownloadResult(
skip_reason="Failed to download PDF from bioRxiv/medRxiv"
)
diff --git a/src/local_deep_research/research_library/downloaders/generic.py b/src/local_deep_research/research_library/downloaders/generic.py
index 4da10d4672..086df4f805 100644
--- a/src/local_deep_research/research_library/downloaders/generic.py
+++ b/src/local_deep_research/research_library/downloaders/generic.py
@@ -71,7 +71,7 @@ class GenericDownloader(BaseDownloader):
pdf_content = super()._download_pdf(pdf_url)
else:
pdf_content = None
- except ValueError:
+ except (ValueError, AttributeError):
# urlparse can raise ValueError for malformed URLs
pdf_content = None
@@ -128,6 +128,7 @@ class GenericDownloader(BaseDownloader):
skip_reason="Could not connect to server - website may be down"
)
except requests.RequestException:
+ logger.warning("Unexpected error checking URL: %s", url)
return DownloadResult(
skip_reason="Network error - could not reach the website"
)
@@ -152,7 +153,7 @@ class GenericDownloader(BaseDownloader):
pdf_content = super()._download_pdf(pdf_url)
else:
pdf_content = None
- except ValueError:
+ except (ValueError, AttributeError):
# urlparse can raise ValueError for malformed URLs
pdf_content = None
diff --git a/src/local_deep_research/research_library/routes/library_routes.py b/src/local_deep_research/research_library/routes/library_routes.py
index 6f81e5d2f0..2d52cd40d8 100644
--- a/src/local_deep_research/research_library/routes/library_routes.py
+++ b/src/local_deep_research/research_library/routes/library_routes.py
@@ -1250,7 +1250,7 @@ def get_research_sources(research_id):
from urllib.parse import urlparse
domain = urlparse(resource.url).hostname or ""
- except ValueError:
+ except (ValueError, AttributeError):
# urlparse can raise ValueError for malformed URLs
pass
diff --git a/src/local_deep_research/research_library/services/library_service.py b/src/local_deep_research/research_library/services/library_service.py
index 205c10ad4b..734abe89e7 100644
--- a/src/local_deep_research/research_library/services/library_service.py
+++ b/src/local_deep_research/research_library/services/library_service.py
@@ -774,7 +774,7 @@ class LibraryService:
try:
return urlparse(url).netloc
- except:
+ except (ValueError, AttributeError):
return ""
def _get_url_hash(self, url: str) -> str:
diff --git a/src/local_deep_research/text_optimization/citation_formatter.py b/src/local_deep_research/text_optimization/citation_formatter.py
index 1ad86cecf9..6669ddf545 100644
--- a/src/local_deep_research/text_optimization/citation_formatter.py
+++ b/src/local_deep_research/text_optimization/citation_formatter.py
@@ -429,7 +429,7 @@ class CitationFormatter:
if len(parts) >= 2:
return ".".join(parts[-2:])
return domain
- except Exception:
+ except (ValueError, AttributeError):
return "source"
@@ -764,7 +764,7 @@ class RISExporter:
else:
# Use domain as publisher
lines.append(f"PB - {domain}")
- except Exception:
+ except (ValueError, AttributeError):
pass
# Y1 - Year accessed (current year)
diff --git a/src/local_deep_research/web/routes/history_routes.py b/src/local_deep_research/web/routes/history_routes.py
index 7658c064af..9f77b9e69e 100644
--- a/src/local_deep_research/web/routes/history_routes.py
+++ b/src/local_deep_research/web/routes/history_routes.py
@@ -2,6 +2,7 @@ import json
import traceback
from flask import Blueprint, jsonify, make_response, session
+from loguru import logger
from ...database.models import ResearchHistory
from ...database.models.library import Document as Document
@@ -80,7 +81,7 @@ def get_history():
try:
metadata = json.loads(item["research_meta"])
item["metadata"] = metadata
- except json.JSONDecodeError:
+ except (json.JSONDecodeError, TypeError):
item["metadata"] = {}
# Ensure timestamps are in ISO format
@@ -218,7 +219,6 @@ def get_research_status(research_id):
@login_required
def get_research_details(research_id):
"""Get detailed progress log for a specific research"""
- from loguru import logger
logger.info(f"Details route accessed for research_id: {research_id}")
diff --git a/src/local_deep_research/web/routes/metrics_routes.py b/src/local_deep_research/web/routes/metrics_routes.py
index e8d59cc3c9..65e14ce795 100644
--- a/src/local_deep_research/web/routes/metrics_routes.py
+++ b/src/local_deep_research/web/routes/metrics_routes.py
@@ -1105,7 +1105,7 @@ def api_research_link_metrics(research_id):
category_counts["Unclassified"] = (
category_counts.get("Unclassified", 0) + 1
)
- except Exception as e:
+ except (AttributeError, KeyError) as e:
logger.debug(f"Error classifying domain {domain}: {e}")
# Sort domains by count
@@ -2172,7 +2172,7 @@ def api_classification_progress():
domain = domain[4:]
if domain:
domains.add(domain)
- except ValueError:
+ except (ValueError, AttributeError):
# urlparse can raise ValueError for malformed URLs
continue
diff --git a/tests/benchmarks/test_benchmark_service.py b/tests/benchmarks/test_benchmark_service.py
index e12524fe04..a6c2acb2c4 100644
--- a/tests/benchmarks/test_benchmark_service.py
+++ b/tests/benchmarks/test_benchmark_service.py
@@ -1428,3 +1428,38 @@ class TestBenchmarkServiceUpdateStatus:
)
assert mock_run.error_message == "Test error"
+
+
+class TestSessionRollbackExceptionHandling:
+ """Tests for session rollback exception handling (PR #2029).
+
+ PR #2029 changed bare `except:` to `except Exception:` in the
+ session.rollback() error handler in sync_pending_results.
+ """
+
+ def test_rollback_exception_is_caught(self):
+ """Exception during rollback is caught and doesn't propagate."""
+ mock_session = Mock()
+ mock_session.rollback.side_effect = Exception("Rollback failed")
+
+ # Simulate the pattern from sync_pending_results
+ caught = False
+ try:
+ mock_session.rollback()
+ except Exception:
+ caught = True
+
+ assert caught
+
+ def test_rollback_success_no_exception(self):
+ """Successful rollback raises no exception."""
+ mock_session = Mock()
+ mock_session.rollback.return_value = None
+
+ error = None
+ try:
+ mock_session.rollback()
+ except Exception as e:
+ error = e
+
+ assert error is None
diff --git a/tests/domain_classifier/test_classifier.py b/tests/domain_classifier/test_classifier.py
index c331ef646d..0bc19bcbbc 100644
--- a/tests/domain_classifier/test_classifier.py
+++ b/tests/domain_classifier/test_classifier.py
@@ -336,3 +336,33 @@ class TestGetCategoriesSummary:
result = classifier.get_categories_summary()
assert result == {}
+
+
+class TestUrlparseValueErrorHandling:
+ """Tests for ValueError handling in urlparse (PR #2012).
+
+ PR #2012 changed bare `except:` to `except ValueError:` when
+ parsing URLs in classify_all_domains.
+ """
+
+ def test_value_error_from_urlparse_caught(self):
+ """ValueError from urlparse is caught with continue."""
+ from urllib.parse import urlparse
+
+ # URLs that could cause issues
+ test_urls = ["https://valid.com/path", "", "not-a-url"]
+ domains = set()
+
+ for url in test_urls:
+ if url:
+ try:
+ parsed = urlparse(url)
+ domain = parsed.netloc.lower()
+ if domain.startswith("www."):
+ domain = domain[4:]
+ if domain:
+ domains.add(domain)
+ except ValueError:
+ continue
+
+ assert "valid.com" in domains
diff --git a/tests/metrics/test_database.py b/tests/metrics/test_database.py
index 407aa0d799..9a222d0526 100644
--- a/tests/metrics/test_database.py
+++ b/tests/metrics/test_database.py
@@ -204,3 +204,32 @@ class TestMetricsDatabaseContextManager:
with pytest.raises(Exception, match="DB Error"):
with db.get_session():
pass
+
+
+class TestFlaskSessionRuntimeError:
+ """Tests for Flask session RuntimeError handling (PR #2014).
+
+ PR #2014 changed bare `except:` to `except RuntimeError:` when
+ accessing flask_session outside request context.
+ """
+
+ def test_runtime_error_caught_outside_flask_context(self):
+ """RuntimeError is raised when accessing Flask session outside context."""
+ with pytest.raises(RuntimeError):
+ from flask import session as flask_session
+
+ flask_session.get("username")
+
+ def test_runtime_error_is_specific_exception(self):
+ """RuntimeError is caught specifically, not by bare except."""
+ caught_type = None
+ try:
+ from flask import session as flask_session
+
+ flask_session.get("username")
+ except RuntimeError:
+ caught_type = "RuntimeError"
+ except Exception:
+ caught_type = "Exception"
+
+ assert caught_type == "RuntimeError"
diff --git a/tests/news/subscription_manager/test_scheduler.py b/tests/news/subscription_manager/test_scheduler.py
index 33abefec30..b56099e588 100644
--- a/tests/news/subscription_manager/test_scheduler.py
+++ b/tests/news/subscription_manager/test_scheduler.py
@@ -1035,3 +1035,36 @@ class TestGetNewsScheduler:
assert scheduler is not None
assert isinstance(scheduler, NewsScheduler)
+
+
+class TestCompletedAtDatetimeParsing:
+ """Tests for completed_at datetime parsing (PR #2029).
+
+ PR #2029 changed bare `except:` to `except ValueError:` in
+ _process_user_documents when parsing completed_at with fromisoformat.
+ """
+
+ def test_valid_iso_datetime_parsed(self):
+ """Valid ISO datetime string is parsed correctly."""
+ completed_at_str = "2024-01-15T10:30:00"
+ completed_at_obj = datetime.fromisoformat(completed_at_str)
+ assert completed_at_obj.year == 2024
+ assert completed_at_obj.month == 1
+
+ def test_iso_datetime_with_z_suffix(self):
+ """ISO datetime with Z suffix is parsed after replacement."""
+ completed_at_str = "2024-01-15T10:30:00Z"
+ completed_at_obj = datetime.fromisoformat(
+ completed_at_str.replace("Z", "+00:00")
+ )
+ assert completed_at_obj is not None
+
+ def test_invalid_datetime_raises_value_error(self):
+ """Invalid datetime string raises ValueError (not caught by bare except)."""
+ with pytest.raises(ValueError):
+ datetime.fromisoformat("not-a-date")
+
+ def test_empty_string_raises_value_error(self):
+ """Empty string raises ValueError."""
+ with pytest.raises(ValueError):
+ datetime.fromisoformat("")
diff --git a/tests/strategies/test_bare_except_fixes.py b/tests/strategies/test_bare_except_fixes.py
new file mode 100644
index 0000000000..0b6c3217cf
--- /dev/null
+++ b/tests/strategies/test_bare_except_fixes.py
@@ -0,0 +1,69 @@
+"""Tests to verify except clauses capture the exception variable and that
+debug logging has been added in strategy modules (PR #2010)."""
+
+import inspect
+
+
+class TestBareExceptFixes:
+ """Verify that except clauses use 'except Exception as e' (not bare
+ 'except Exception:') and that debug logging is present."""
+
+ def test_synonym_expansion_captures_exception(self):
+ from local_deep_research.advanced_search_system.candidate_exploration.adaptive_explorer import (
+ AdaptiveExplorer,
+ )
+
+ source = inspect.getsource(AdaptiveExplorer._synonym_expansion_query)
+ assert "except Exception as e" in source, (
+ "_synonym_expansion_query should use 'except Exception as e'"
+ )
+
+ def test_synonym_expansion_logs_error(self):
+ from local_deep_research.advanced_search_system.candidate_exploration.adaptive_explorer import (
+ AdaptiveExplorer,
+ )
+
+ source = inspect.getsource(AdaptiveExplorer._synonym_expansion_query)
+ assert "logger.debug" in source, (
+ "_synonym_expansion_query should have logger.debug for exceptions"
+ )
+
+ def test_related_terms_captures_exception(self):
+ from local_deep_research.advanced_search_system.candidate_exploration.adaptive_explorer import (
+ AdaptiveExplorer,
+ )
+
+ source = inspect.getsource(AdaptiveExplorer._related_terms_query)
+ assert "except Exception as e" in source, (
+ "_related_terms_query should use 'except Exception as e'"
+ )
+
+ def test_related_terms_logs_error(self):
+ from local_deep_research.advanced_search_system.candidate_exploration.adaptive_explorer import (
+ AdaptiveExplorer,
+ )
+
+ source = inspect.getsource(AdaptiveExplorer._related_terms_query)
+ assert "logger.debug" in source, (
+ "_related_terms_query should have logger.debug for exceptions"
+ )
+
+ def test_get_synonyms_captures_exception(self):
+ from local_deep_research.advanced_search_system.strategies.evidence_based_strategy_v2 import (
+ EnhancedEvidenceBasedStrategy,
+ )
+
+ source = inspect.getsource(EnhancedEvidenceBasedStrategy._get_synonyms)
+ assert "except Exception as e" in source, (
+ "_get_synonyms should use 'except Exception as e'"
+ )
+
+ def test_get_synonyms_logs_error(self):
+ from local_deep_research.advanced_search_system.strategies.evidence_based_strategy_v2 import (
+ EnhancedEvidenceBasedStrategy,
+ )
+
+ source = inspect.getsource(EnhancedEvidenceBasedStrategy._get_synonyms)
+ assert "logger.debug" in source, (
+ "_get_synonyms should have logger.debug for exceptions"
+ )