Skip to content

Downgrade some expected "already exists" errors to info-level logging (#958)#1075

Merged
MelissaAutumn merged 5 commits into
mainfrom
issue-958-dont-log-to-sentry
Jul 9, 2026
Merged

Downgrade some expected "already exists" errors to info-level logging (#958)#1075
MelissaAutumn merged 5 commits into
mainfrom
issue-958-dont-log-to-sentry

Conversation

@markstos

@markstos markstos commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

For web/db apps, a common source of exceptions stem from users double-clicking
some button or link to add a thing when it only needs a single click.

In these cases, the first request will trigger a successful addition and the second
one will trigger some kind of "already exists" condition, which is harmless to ignore.

For some known cases like that, we downgrade them from throwing exceptions to
info-level logging.

Other cases of adding things where we still would not expect exceptions to be
triggered reamin as exceptions.

Applicable Issues

QA Log

  • Automated analysis of places that need to be updated, including those flagged by
    Sentry.
  • Added automated test coverage, focusing on high-value test cases.

@markstos markstos requested a review from MelissaAutumn July 7, 2026 18:32

@MelissaAutumn MelissaAutumn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good start, I'd like to see those private functions dropped and code moved inside our existing classes where it makes sense.

Comment thread src/thunderbird_accounts/authentication/api.py Outdated
Comment thread src/thunderbird_accounts/authentication/clients.py Outdated
POSTGRES_DUPLICATE_KEY_MARKER = 'duplicate key value violates unique constraint'


def is_already_exists_error(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This can live inside the KeycloakError class. No need for it to live in outside.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The is_already_exists_error was put in the global scope with the understanding that the pattern of generating exceptions from double-click-to-insert affects all things we insert-- the exception we noticed related to keycloak happens to be the first one. As we scale, the pattern will repeat for everything we insert, see note below.

But in the spirit of YAGNI, I've moved it into the narrowest scope where it's actually used now, the ImportUserError class. Later when we hit similar cases and refactor to handle those, we can refactor to move this back to a shared location.

Comment thread src/thunderbird_accounts/mail/views.py Outdated
Comment thread src/thunderbird_accounts/mail/views.py
Comment thread src/thunderbird_accounts/mail/views.py
return True

error_text = ' '.join(filter(None, [error_code, error_desc, error])).lower()
return POSTGRES_DUPLICATE_KEY_MARKER in error_text

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this something we hit? I don't remember duplicate key errors here, but also I wasn't really looking at the response other than status code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That addition was experience-based rather than evidence-based. Having managed busy Postgres-backed websites in the past, at scale that became a common pattern: The user's double-click behavior would lead to a race condition to insert twice. One insert would win and the other would throw the "duplicate key" error.

We could remove it because we haven't hit it yet, but based on the scale we are trying to build we likely will. Also, it seems is the best solution: there's not a reasonable way to prevent the condition from happening: the best path seems to be accept that we'll trigger the duplicate key violation periodically, then explicitly accept this condition as OK in cases were we expect them to be safe, like a double-click for inserting record targeted here.

@markstos

markstos commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

Feedback has been addressed in a fixup commit that can be reviewed and then squashed during merging.

@MelissaAutumn MelissaAutumn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I believe we have a linting error now 😄

And a suggestion to be slightly more narrow in the exception usage. Otherwise good stuff.

Comment thread src/thunderbird_accounts/authentication/api.py Outdated
Comment thread src/thunderbird_accounts/authentication/exceptions.py Outdated
markstos added 5 commits July 9, 2026 17:39
…#958)

For web/db apps, a common source of exceptions stem from users double-clicking
some button or link to add a thing when it only needs a single click.

In these cases, the first request will trigger a successful addition and the second
one will trigger some kind of "already exists" condition, which is harmless to ignore.

For some known cases like that, we downgrade them from throwing exceptions to
info-level logging.

Other cases of adding things where we still would not expect exceptions to be
triggered reamin as exceptions.
- Re-organize code
- Remove necessary logging
@markstos markstos force-pushed the issue-958-dont-log-to-sentry branch from 6ecd2d1 to 1e450ed Compare July 9, 2026 21:53
@markstos markstos requested a review from MelissaAutumn July 9, 2026 21:53
@MelissaAutumn

Copy link
Copy Markdown
Member

Thanks!

@MelissaAutumn MelissaAutumn merged commit ffa53fb into main Jul 9, 2026
8 checks passed
@MelissaAutumn MelissaAutumn deleted the issue-958-dont-log-to-sentry branch July 9, 2026 22:21
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.

Don't log "email already exist" errors for alias/sign-up form

2 participants