Skip to content

Improve error messages when database access is disposed (#174)#474

Open
konard wants to merge 4 commits into
mainfrom
issue-174-38feee43
Open

Improve error messages when database access is disposed (#174)#474
konard wants to merge 4 commits into
mainfrom
issue-174-38feee43

Conversation

@konard
Copy link
Copy Markdown
Member

@konard konard commented Sep 13, 2025

Summary

This PR fixes GitHub issue #174 by implementing better error messages when database access is disposed right after the access is created.

Problem

Previously, when a Links instance was disposed and users tried to access database methods (Count(), Create(), Update(), Delete(), Each()), they would encounter:

  • Generic null reference exceptions
  • Access violations
  • Non-descriptive error messages
  • Crashes with unclear stack traces

This made debugging difficult for users who accidentally tried to use disposed database connections.

Solution

Added comprehensive disposal checking to all public database access methods across all Links implementations:

Key Changes:

  • Added EnsureNotDisposed() method to all Links base classes:

    • UnitedMemoryLinksBase<T>
    • SplitMemoryLinksBase<T>
    • Links<T> (FFI)
    • UInt32Links (FFI)
  • Clear, descriptive error message:

    "Cannot access a disposed Links instance. The database connection has been closed and is no longer available for operations."
    
  • Applied to all public methods:

    • Count(IList<TLinkAddress>? restriction)
    • Create(IList<TLinkAddress>? substitution, WriteHandler<TLinkAddress>? handler)
    • Update(IList<TLinkAddress>? restriction, IList<TLinkAddress>? substitution, WriteHandler<TLinkAddress>? handler)
    • Delete(IList<TLinkAddress>? restriction, WriteHandler<TLinkAddress>? handler)
    • Each(IList<TLinkAddress>? restriction, ReadHandler<TLinkAddress>? handler)

Testing

  • Added comprehensive test suite (DisposalErrorMessagesTests.cs) that verifies:
    • All methods throw ObjectDisposedException when called on disposed instances
    • Exception messages contain meaningful descriptions
    • Object names are correctly included in exceptions
    • Non-disposed instances continue to work normally
  • All existing tests pass - no regressions introduced

Example Usage

Before (unclear error):

var links = new UnitedMemoryLinks<uint>(memory);
links.Dispose();
var count = links.Count(); // NullReferenceException or similar

After (clear error):

var links = new UnitedMemoryLinks<uint>(memory);
links.Dispose();
var count = links.Count(); 
// ObjectDisposedException: Cannot access a disposed Links instance. 
// The database connection has been closed and is no longer available for operations.

Impact

  • Better Developer Experience: Users get immediate, clear feedback when they accidentally use disposed connections
  • Easier Debugging: Error messages explain exactly what's wrong and what needs to be fixed
  • Consistent Behavior: All Links implementations now behave the same way when disposed
  • Zero Breaking Changes: Only adds new error checking, doesn't change existing functionality

Test plan

  • All existing unit tests pass
  • New disposal error tests pass
  • Manual testing with disposed instances shows improved error messages
  • Build succeeds with no warnings or errors

🤖 Generated with Claude Code


Resolves #174

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #174
@konard konard self-assigned this Sep 13, 2025
Previously, when a Links instance was disposed and users tried to access it,
they would receive generic null reference exceptions or other non-descriptive
errors. This made it difficult to understand what went wrong.

This commit adds proper ObjectDisposedException handling to all public
database access methods with a clear, descriptive error message:

- Added EnsureNotDisposed() method to all Links base classes
- Throws ObjectDisposedException with meaningful message:
  "Cannot access a disposed Links instance. The database connection
  has been closed and is no longer available for operations."
- Applied to all public methods: Count(), Create(), Update(), Delete(), Each()
- Covers all implementations: UnitedMemoryLinksBase, SplitMemoryLinksBase,
  FFI Links, and UInt32Links
- Added comprehensive tests to verify the fix works correctly

Now when users accidentally access disposed Links instances, they get clear
error messages that explain the problem and help them understand what
needs to be fixed in their code.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] Better error messages when database access is disposed right after the access is created Improve error messages when database access is disposed (#174) Sep 13, 2025
@konard konard marked this pull request as ready for review September 13, 2025 19:29
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <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.

Better error messages when database access is disposed right after the access is created

1 participant