Skip to content

fix: Replace bare except clauses with specific exception types#77

Open
robbybarnes wants to merge 2 commits intojmdevita:mainfrom
robbybarnes:feature/improve-error-handling
Open

fix: Replace bare except clauses with specific exception types#77
robbybarnes wants to merge 2 commits intojmdevita:mainfrom
robbybarnes:feature/improve-error-handling

Conversation

@robbybarnes
Copy link

Summary

Replaces bare except: clauses with specific exception types throughout the codebase for better error handling and debugging.

Motivation

Bare except: clauses are a Python anti-pattern because they:

  • Catch SystemExit, KeyboardInterrupt, and other exceptions that shouldn't be silently handled
  • Make debugging difficult by hiding the actual error type
  • Can mask programming errors (e.g., NameError, TypeError)
  • Violate PEP 8 style guidelines

Changes

coordinator.py

  • except:except (ValueError, TypeError): for datetime parsing
  • except:except (aiohttp.ClientError, json.JSONDecodeError, TimeoutError): for API calls
  • Added aiohttp import for proper exception handling

sensor.py

  • except:except (KeyError, IndexError): for data extraction
  • except:except (ValueError, TypeError, AttributeError): for date parsing

utils.py

  • except:except (ValueError, TypeError, AttributeError): for date parsing fallbacks

tests/test_coordinator.py

  • Added mock for carrier codes endpoint (previously relied on bare except catching mock errors)

Testing

  • All tests pass (11/11)
  • Test coverage improved by properly mocking all API endpoints

robbybarnes and others added 2 commits January 20, 2026 18:26
Replaces all bare `except:` clauses with specific exception types to improve
error handling and make debugging easier. Bare except clauses can hide bugs
and catch unexpected exceptions like KeyboardInterrupt or SystemExit.

Changes:
- coordinator.py: Added aiohttp import, replaced bare except with specific types
  - datetime.strptime: ValueError, TypeError
  - HTTP/JSON operations: aiohttp.ClientError, json.JSONDecodeError, TimeoutError
- sensor.py: Replaced bare except with appropriate types
  - Dictionary/list access: KeyError, IndexError, TypeError
  - datetime.fromtimestamp: ValueError, TypeError, OSError
- utils.py: Replaced bare except with specific types for date parsing
  - datetime.fromisoformat: ValueError, TypeError, AttributeError
  - dateutil.parser.parse: ValueError, TypeError, AttributeError

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The test was missing a mock for the carrier codes endpoint. Previously
the bare except clause was catching the mock framework's AssertionError,
but now with specific exception handling this needs proper mocking.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant