Skip to content

database error handling#15

Open
luuisabelle wants to merge 6 commits intodevfrom
db-error-handling
Open

database error handling#15
luuisabelle wants to merge 6 commits intodevfrom
db-error-handling

Conversation

@luuisabelle
Copy link
Copy Markdown

No description provided.

@luuisabelle
Copy link
Copy Markdown
Author

it looks like the database schema was changed very recently so I'll make sure to resolve any conflicts soon

Comment on lines +224 to +228
if record is None:
raise RecordNotFoundError(f"No snack found with SKU {sku}")
return Snack(**record)
except sqlite3.Error as e:
raise DatabaseError(f"Database error in fetching snack {sku}: {str(e)}")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is raising a RecordNotFoundError but isn't being caught because the catch block is only catching sqlite3.Error

cursor.execute("""SELECT sku FROM snacks WHERE sku = ?""", (snack.sku,))
existing = cursor.fetchone()
if existing is not None:
raise DuplicateRecordError(f"Snack with SKU {snack.sku} already exists")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is raising a DuplicateRecordError but the catch block below is only catching sqlite3.Error instances

Comment on lines +135 to +138
raise RecordNotFoundError(f"No snack found with SKU {sku}")
return Snack(**record)
except sqlite3.Error as e:
raise DatabaseError(f"Database error when fetching snack {sku}: {str(e)}")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is raising a RecordNotFoundError but isn't being caught because the catch block is only catching sqlite3.Error

Comment on lines +103 to +107
if record is None:
raise RecordNotFoundError(f"No snack found with SKU: {sku}" )
return Snack(**record)
except sqlite3.Error as e:
raise DatabaseError(f"Database error when fetching snack {sku}: {str(e)}")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is raising a RecordNotFoundError but isn't being caught because the catch block is only catching sqlite3.Error

connection.row_factory = sqlite3.Row
return connection
except sqlite3.Error as e:
raise ConnectionError(f"Failed to connect to database: {str(e)}")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't think this is being caught in any catch blocks (since all the catch blocks are catching sqlite3.Error)

Copy link
Copy Markdown
Contributor

@NicholasLe04 NicholasLe04 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great work so far! left some requested changes.

Comment on lines +106 to +109
except (sqlite3.Error, DatabaseError, ConnectionError, DatabaseInitError) as e:
raise DatabaseError(f"Database error when fetching snack {sku}: {str(e)}")
except (RecordNotFoundError) as e:
raise RecordNotFoundError(str(e))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the second catch block is actually unreachable because RecordNotFoundError is a subclass of DatabaseError which is caught first

Suggested change
except (sqlite3.Error, DatabaseError, ConnectionError, DatabaseInitError) as e:
raise DatabaseError(f"Database error when fetching snack {sku}: {str(e)}")
except (RecordNotFoundError) as e:
raise RecordNotFoundError(str(e))
except RecordNotFoundError as e:
raise RecordNotFoundError(str(e))
except Exception as e:
raise DatabaseError(f"Database error when fetching snack {sku}: {str(e)}")

Comment on lines +139 to +142
except (sqlite3.Error, DatabaseError, ConnectionError, DatabaseInitError) as e:
raise DatabaseError(f"Database error when fetching snack {sku}: {str(e)}")
except (RecordNotFoundError) as e:
raise RecordNotFoundError(str(e))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the second catch block is actually unreachable because RecordNotFoundError is a subclass of DatabaseError which is caught first

Suggested change
except (sqlite3.Error, DatabaseError, ConnectionError, DatabaseInitError) as e:
raise DatabaseError(f"Database error when fetching snack {sku}: {str(e)}")
except (RecordNotFoundError) as e:
raise RecordNotFoundError(str(e))
except RecordNotFoundError as e:
raise RecordNotFoundError(str(e))
except Exception as e:
raise DatabaseError(f"Database error when deleting snack {sku}: {str(e)}")

Comment on lines +184 to +187
except (sqlite3.Error, DatabaseError, ConnectionError, DatabaseInitError) as e:
raise DatabaseError(f"Database error when fetching snack {snack.sku}: {str(e)}")
except (DuplicateRecordError) as e:
raise DuplicateRecordError(str(e))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the second catch block is actually unreachable because DuplicateRecordError is a subclass of DatabaseError which is caught first

Suggested change
except (sqlite3.Error, DatabaseError, ConnectionError, DatabaseInitError) as e:
raise DatabaseError(f"Database error when fetching snack {snack.sku}: {str(e)}")
except (DuplicateRecordError) as e:
raise DuplicateRecordError(str(e))
except DuplicateRecordError as e:
raise DuplicateRecordError(str(e))
except Exception as e:
raise DatabaseError(f"Database error when creating snack {snack.sku}: {str(e)}")

Comment on lines +233 to +236
except (sqlite3.Error, DatabaseError, ConnectionError, DatabaseInitError) as e:
raise DatabaseError(f"Database error in fetching snack {sku}: {str(e)}")
except (RecordNotFoundError) as e:
raise RecordNotFoundError(str(e))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the second catch block is actually unreachable because RecordNotFoundError is a subclass of DatabaseError which is caught first

Suggested change
except (sqlite3.Error, DatabaseError, ConnectionError, DatabaseInitError) as e:
raise DatabaseError(f"Database error in fetching snack {sku}: {str(e)}")
except (RecordNotFoundError) as e:
raise RecordNotFoundError(str(e))
except RecordNotFoundError as e:
raise RecordNotFoundError(str(e))
except Exception as e:
raise DatabaseError(f"Database error in updating snack {sku}: {str(e)}")

@luuisabelle luuisabelle requested a review from NicholasLe04 April 7, 2025 23:31
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.

3 participants