Skip to content

Commit 1bdaee9

Browse files
fix: replace bare except clauses with except Exception (#344)
Bare `except:` catches BaseException including KeyboardInterrupt and SystemExit. This replaces 9 bare except clauses with `except Exception:` to only catch application-level exceptions. Co-authored-by: haosenwang1018 <haosenwang1018@users.noreply.github.com>
1 parent deac2dd commit 1bdaee9

5 files changed

Lines changed: 9 additions & 9 deletions

File tree

opencontext/context_consumption/generation/smart_todo_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def generate_todo_tasks(self, start_time: int, end_time: int) -> Optional[str]:
9292
deadline = datetime.datetime.strptime(deadline_str, "%Y-%m-%d %H:%M")
9393
else:
9494
deadline = datetime.datetime.strptime(task["due_date"], "%Y-%m-%d")
95-
except:
95+
except Exception:
9696
pass
9797

9898
todo_id = get_storage().insert_todo(

opencontext/llm/llm_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def _extract_error_summary(error: Any) -> str:
439439
if ". Request id:" in actual_msg:
440440
actual_msg = actual_msg.split(". Request id:")[0]
441441
return actual_msg
442-
except:
442+
except Exception:
443443
pass
444444
return f"Error {code}"
445445

opencontext/monitoring/metrics_collector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def wrapper(*args, **kwargs):
4848
if hasattr(result, "__len__") and not isinstance(result, str):
4949
try:
5050
context_count = len(result)
51-
except:
51+
except Exception:
5252
context_count = 1
5353

5454
monitor.record_processing_metrics(
@@ -94,7 +94,7 @@ def wrapper(*args, **kwargs):
9494
elif hasattr(result, "__len__") and not isinstance(result, str):
9595
try:
9696
snippets_count = len(result)
97-
except:
97+
except Exception:
9898
snippets_count = 0
9999

100100
# 尝试从参数中获取query

opencontext/storage/backends/sqlite_backend.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ def save_monitoring_token_usage(
951951
logger.error(f"Failed to save token usage: {e}")
952952
try:
953953
self.connection.rollback()
954-
except:
954+
except Exception:
955955
pass
956956
return False
957957

@@ -1047,7 +1047,7 @@ def save_monitoring_stage_timing(
10471047
logger.error(f"Failed to save stage timing: {e}")
10481048
try:
10491049
self.connection.rollback()
1050-
except:
1050+
except Exception:
10511051
pass
10521052
return False
10531053

@@ -1087,7 +1087,7 @@ def save_monitoring_data_stats(
10871087
logger.error(f"Failed to save data stats: {e}")
10881088
try:
10891089
self.connection.rollback()
1090-
except:
1090+
except Exception:
10911091
pass
10921092
return False
10931093

@@ -1313,7 +1313,7 @@ def cleanup_old_monitoring_data(self, days: int = 7) -> bool:
13131313
logger.error(f"Failed to cleanup old monitoring data: {e}")
13141314
try:
13151315
self.connection.rollback()
1316-
except:
1316+
except Exception:
13171317
pass
13181318
return False
13191319

opencontext/utils/json_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,5 @@ def fix_quotes_in_match(match):
101101
try:
102102
fixed = re.sub(pattern, fix_quotes_in_match, json_str)
103103
return fixed
104-
except:
104+
except Exception:
105105
return json_str

0 commit comments

Comments
 (0)