AOSSIE-Org / AOSSIE-Org/PictoPy

BUG: Improve SQLite error handling in get_db_connection

Open
#1,102 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
283
Forks
679
Avg merge
7d 2h
Merged PRs (30d)
3

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### What happened?

### Problem

The current implementation of `get_db_connection` in `backend/app/database/connection.py` catches a broad `Exception` when handling database operations.

Catching all exceptions can:
- Hide SQLite-specific errors
- Make debugging more difficult
- Handle unrelated runtime errors in a place meant only for database failures
- Provide no logging context when a transaction fails

### Why this matters

More precise error handling improves:
- Debuggability for contributors
- Code clarity and maintainability
- Reliability of database transaction management
- Alignment with Python and database best practices

### Proposed improvement

Refine the exception handling to:
- Catch `sqlite3.Error` instead of a generic `Exception`
- Log database-related failures for better visibility
- Preserve the existing rollback and re-raise behavior

This change does not affect application logic but improves robustness and developer experience.

I would be happy to open a PR with this improvement if maintainers agree.

### Record

- [x] I agree to follow this project's Code of Conduct

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.