f322052cc6
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).
26 lines
1.1 KiB
Python
26 lines
1.1 KiB
Python
"""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" |