Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] fix(gaq): support localized error messages #24457

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

villebro
Copy link
Member

@villebro villebro commented Jun 20, 2023

SUMMARY

Use the user's locale to correctly handle flask-babel translated strings, both regular gettext and lazy_gettext.

Related PR which started discussions that led to this PR: #24447

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@@ -96,15 +101,13 @@ def load_chart_data_into_cache(
else str(ex)
)
errors = [{"message": error}]
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this code is repeated; feels like I've seen errors reported as {"message": "(some exception message)"} fairly frequently so maybe it could be more DRY and be made safe against lazy messaging for good by reusing any logic we need surrounding that.

Presumably even with locale here, we still need to be using gettext instead of gettext_lazy when raising the error we've caught here; having the locale context will let us translate the message correctly but won't fix the fact that the json library doesn't know how to serialise LazyString.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea. I'm still contemplating different solutions to this problem, as there's multiple things that are wrong right now:

  1. The absence of a locale in celery tasks (which this is primarily trying to address)
  2. Incorrect use of lazy_gettext where gettext is sufficient
  3. serializing/deserializing using json.

Thinking more about this I'm not so sure anymore if we should really be using flask.json as I've done here (bullet nr 3). So maybe that part should be reverted, and also that error message logic DRY'd up like you are proposing.

Copy link
Contributor

@giftig giftig Jun 21, 2023

Choose a reason for hiding this comment

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

Ah I didn't spot the import change to use flask.json. So flask's json implementation most likely will be able to deal with LazyString, actually.

Copy link
Member Author

Choose a reason for hiding this comment

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

It does. However, similar logic may exist elsewhere, so we may run into this same issue again. So at any rate, we should ensure prudent use of gettext/lazy_gettext everywhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants