refactor(rag_engine): narrow 'except Exception' in _get_sentence_transformers (Phase 13 site 1)
Site 1 (BC at L33) was:
except Exception as e:
sys.stderr.write(f'FAILED to import sentence_transformers: {e}')
sys.stderr.flush()
raise e
Per TIER1_REVIEW: catch + log + re-raise is Pattern 2 of the styleguide.
The fix is to narrow the except to specific exception types that
sentence_transformers could raise on import (ImportError, AttributeError).
Refactored to:
except (ImportError, AttributeError) as e:
sys.stderr.write(f'FAILED to import sentence_transformers: {e}')
sys.stderr.flush()
raise
The bare 'raise' re-raises the current exception being handled,
preserving the original type and traceback. (Replaces 'raise e' which
raised a specific value but lost the traceback context.)
Audit: rag_engine BC 5 -> 4. RETHROW +1 (the narrowed except is now
classified as Pattern 3 catch+re-raise; strict mode accepts).
This commit is contained in:
+2
-2
@@ -30,10 +30,10 @@ def _get_sentence_transformers():
|
||||
if e.name == "sentence_transformers":
|
||||
raise ImportError(LOCAL_RAG_INSTALL_HINT) from e
|
||||
raise
|
||||
except Exception as e:
|
||||
except (ImportError, AttributeError) as e:
|
||||
sys.stderr.write(f"FAILED to import sentence_transformers: {e}\n")
|
||||
sys.stderr.flush()
|
||||
raise e
|
||||
raise
|
||||
return _SENTENCE_TRANSFORMERS
|
||||
|
||||
def _get_google_genai():
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
"""Phase 13 site 1: narrow 'except Exception' in _get_sentence_transformers.
|
||||
|
||||
Site 1 (BC at L33): the second except in the try/except chain is broad:
|
||||
except Exception as e:
|
||||
sys.stderr.write(...)
|
||||
sys.stderr.flush()
|
||||
raise e
|
||||
|
||||
Per TIER1_REVIEW: catch + log + re-raise is Pattern 2 of the styleguide.
|
||||
The fix is to narrow the except to specific exception types that
|
||||
sentence_transformers might raise on import (ImportError, AttributeError).
|
||||
"""
|
||||
import sys
|
||||
sys.path.insert(0, ".")
|
||||
|
||||
|
||||
def test_phase13_site1_get_sentence_transformers_narrow():
|
||||
import inspect
|
||||
import src.rag_engine
|
||||
src_text = inspect.getsource(src.rag_engine._get_sentence_transformers)
|
||||
# Must NOT have 'except Exception as e:' (broad catch)
|
||||
assert "except Exception as e:" not in src_text, \
|
||||
"_get_sentence_transformers must narrow 'except Exception'"
|
||||
# Should have a narrow except for module-loading failures
|
||||
assert "except ImportError" in src_text or "except (ImportError" in src_text, \
|
||||
"_get_sentence_transformers should have narrow ImportError/AttributeError catch"
|
||||
Reference in New Issue
Block a user