fix: replace bare except blocks with specific exception types#1591
fix: replace bare except blocks with specific exception types#1591giwaov wants to merge 1 commit intogenlayerlabs:mainfrom
Conversation
Replace all bare 'except:' blocks in the backend with specific exception types to avoid silently swallowing unexpected errors like KeyboardInterrupt, SystemExit, or MemoryError. Changes: - worker_service.py: catch (psutil.Error, OSError) for process metrics - transactions_processor.py: catch (ValueError, TypeError) for address checksum conversion - providers.py: catch (OSError, ValueError) for socket connection check - fastapi_endpoint_generator.py: catch Exception for rollback errors - migration script: catch (json.JSONDecodeError, TypeError) for JSON parsing Contributes to genlayerlabs#313
📝 WalkthroughWalkthroughThis PR systematically tightens exception handling across five backend files by replacing broad bare Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
backend/database_handler/transactions_processor.py (1)
901-904: Pattern at line 903 is correct and complete.The change to
except (ValueError, TypeError):correctly captures the two exceptions thatto_checksum_address()can raise:
TypeError: when input is not a string or bytes (e.g.,None,int)ValueError: when input is a string/bytes but not a valid Ethereum address (wrong length, non-hex, invalid EIP-55 checksum)However, note that line 1385 in the same file is incomplete — it catches only
ValueErrorand will missTypeError:except ValueError: # Line 1385 - should also catch TypeError checksum_address = addressConsider updating line 1385 to match the pattern:
except (ValueError, TypeError):. Lines 922, 1422, and 1454 use overly broadExceptioncatches, which work but are less precise.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@backend/database_handler/transactions_processor.py` around lines 901 - 904, One occurrence of checksum address conversion uses except ValueError only when calling self.web3.to_checksum_address(address) (the checksum_address assignment block) which will miss TypeError cases; change that except ValueError to except (ValueError, TypeError) to match the earlier correct pattern used where checksum_address = self.web3.to_checksum_address(address) is guarded, and while here you’re editing the except clause for checksum_address ensure any other broad except Exception blocks around checksum/address conversion (the handlers near the other checksum_address assignments) are reviewed and narrowed where possible (prefer catching ValueError and TypeError specifically).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@backend/database_handler/transactions_processor.py`:
- Around line 901-904: One occurrence of checksum address conversion uses except
ValueError only when calling self.web3.to_checksum_address(address) (the
checksum_address assignment block) which will miss TypeError cases; change that
except ValueError to except (ValueError, TypeError) to match the earlier correct
pattern used where checksum_address = self.web3.to_checksum_address(address) is
guarded, and while here you’re editing the except clause for checksum_address
ensure any other broad except Exception blocks around checksum/address
conversion (the handlers near the other checksum_address assignments) are
reviewed and narrowed where possible (prefer catching ValueError and TypeError
specifically).
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: c7749154-3417-4d58-aee5-5dfd14d981d9
📒 Files selected for processing (5)
backend/consensus/worker_service.pybackend/database_handler/migration/versions/02aa0c34a463_validator_config_string_to_jsonb.pybackend/database_handler/transactions_processor.pybackend/node/create_nodes/providers.pybackend/protocol_rpc/fastapi_endpoint_generator.py
Summary
Replaces all bare \except:\ blocks in the backend with specific exception types to prevent silently swallowing unexpected errors like \KeyboardInterrupt, \SystemExit, or \MemoryError.
Changes
Related Issue
Contributes to #313
Summary by CodeRabbit