Skip to content

Add structured exception handling for lock operations#21

Merged
antonkomarev merged 3 commits into
masterfrom
feature/structured-exception-handling
Feb 8, 2026
Merged

Add structured exception handling for lock operations#21
antonkomarev merged 3 commits into
masterfrom
feature/structured-exception-handling

Conversation

@antonkomarev
Copy link
Copy Markdown
Member

@antonkomarev antonkomarev commented Feb 8, 2026

Introduces a dedicated exception hierarchy for database lock operations, improving error handling clarity and providing better context for debugging lock-related failures.

Changes

New Exception Classes

  • AbstractLockException — Base exception for all database lock-related errors

    • Thrown only for exceptional situations (database errors, connection issues)
    • NOT thrown for normal lock contention (methods return false instead)
  • LockAcquireException — Thrown when lock acquisition fails due to database error

    • Includes static factory method fromDatabaseError() with lock key context
    • Wraps original exception for full error chain
  • LockReleaseException — Thrown when lock release fails due to database error

    • Includes static factory method fromDatabaseError() with lock key context
    • Wraps original exception for full error chain

Updated Logic in PostgresAdvisoryLocker

  • Lock acquisition/release methods now throw structured exceptions on database errors
  • Exception messages include human-readable lock key for easier debugging
  • Distinguishes between "lock unavailable" (returns false) vs "database error" (throws exception)

Test Coverage

  • New test suite: PostgresAdvisoryLockerExceptionTest with 172 lines
  • Validates exception throwing for acquire/release failures
  • Tests exception context (messages, previous exception chain)

Documentation

  • Updated CLAUDE.md with exception handling architecture notes

\Throwable catches both \Exception and \Error. Catching \Error
(TypeError, OutOfMemoryError, etc.) and wrapping it in a domain
\Exception is incorrect — these are programming errors, not
recoverable database exceptions.

\Throwable is preserved only in withinSessionLevelLock() where it
guards the user callback and the finally block.
@antonkomarev antonkomarev merged commit ea10c91 into master Feb 8, 2026
2 checks passed
@antonkomarev antonkomarev deleted the feature/structured-exception-handling branch February 8, 2026 13:38
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