Skip to content

fix: translate integrity errors from the message upsert - #60

Merged
lesnik512 merged 1 commit into
mainfrom
fix/message-upsert-error-translation
Oct 17, 2026
Merged

lesnik512 merged 1 commit into
mainfrom
fix/message-upsert-error-translation

Conversation

@lesnik512

Copy link
Copy Markdown
Member

MessagesRepository.get_or_create (from #57) calls session.execute directly, so a constraint violation reached the caller as SQLAlchemy's IntegrityError, which the API turns into a 500. The repository's own methods translate these into ForeignKeyError (400) or DuplicateKeyError (409). The upsert now goes through wrap_sqlalchemy_exception(error_messages=DEFAULT_ERROR_MESSAGE_TEMPLATES, dialect_name="postgresql"), the same as ChatsRepository.get_or_create_direct in #58.

The use case runs its membership check first, so no request can currently reach a foreign-key violation here. The new test therefore calls the repository directly with an unknown chat_id, and on main it fails with the untranslated error.

Verified locally against Postgres 17 with ruff format, ruff check, ty check, and the full suite (141 passed, 100% coverage).

get_or_create called session.execute directly, so a constraint violation
surfaced as SQLAlchemy's IntegrityError instead of advanced-alchemy's
ForeignKeyError or DuplicateKeyError, which the API maps to 400 and 409.
@lesnik512
lesnik512 merged commit ba925ef into main Oct 17, 2026
4 checks passed
@lesnik512
lesnik512 deleted the fix/message-upsert-error-translation branch October 17, 2026 07:59
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