Handle invalid locale configuration graciously during startup #6517

Merged
Jackenmen merged 2 commits from handle_invalid_locale_graciously into V3/develop 2025-02-06 01:42:43 +01:00
Jackenmen commented 2025-02-03 05:02:20 +01:00 (Migrated from github.com)

Description of the changes

Fix startup issue on invalid locale configuration:

[2025-02-02 22:09:10] [CRITICAL] red.main: The main bot task didn't handle an exception and has crashed
Traceback (most recent call last):
  File "/data/venv/lib/python3.11/site-packages/redbot/core/_i18n.py", line 40, in _get_standardized_locale_name
    locale = Locale.parse(language_code, sep="-")
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/venv/lib/python3.11/site-packages/babel/core.py", line 349, in parse
    raise ValueError(msg)  # `parse_locale` would raise a ValueError, so let's do that here
    ^^^^^^^^^^^^^^^^^^^^^
ValueError: ("Empty locale identifier value: ''\n\nIf you didn't explicitly pass an empty value to a Babel function, this could be caused by there being no suitable locale environment variables for the API you tried to use.",)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/data/venv/lib/python3.11/site-packages/redbot/__main__.py", line 470, in red_exception_handler
    red_task.result()
  File "/data/venv/lib/python3.11/site-packages/redbot/__main__.py", line 369, in run_bot
    await red.start(token)
  File "/data/venv/lib/python3.11/site-packages/redbot/core/bot.py", line 1278, in start
    await self._pre_login()
  File "/data/venv/lib/python3.11/site-packages/redbot/core/bot.py", line 1150, in _pre_login
    _i18n.set_global_regional_format(i18n_regional_format)
  File "/data/venv/lib/python3.11/site-packages/redbot/core/_i18n.py", line 60, in set_global_regional_format
    language_code = _get_standardized_locale_name(language_code)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/venv/lib/python3.11/site-packages/redbot/core/_i18n.py", line 42, in _get_standardized_locale_name
    raise ValueError("Invalid language code. Use format: `en-US`")
ValueError: Invalid language code. Use format: `en-US`

with config such as:

... "locale": "en-US", "regional_format": ""}

While an empty string is not something that could have been set by Core, a previously public i18n API for setting locale did not validate its input and as such, some users could have such values in their config files.

Have the changes in this PR been tested?

No

### Description of the changes Fix startup issue on invalid locale configuration: ``` [2025-02-02 22:09:10] [CRITICAL] red.main: The main bot task didn't handle an exception and has crashed Traceback (most recent call last): File "/data/venv/lib/python3.11/site-packages/redbot/core/_i18n.py", line 40, in _get_standardized_locale_name locale = Locale.parse(language_code, sep="-") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/data/venv/lib/python3.11/site-packages/babel/core.py", line 349, in parse raise ValueError(msg) # `parse_locale` would raise a ValueError, so let's do that here ^^^^^^^^^^^^^^^^^^^^^ ValueError: ("Empty locale identifier value: ''\n\nIf you didn't explicitly pass an empty value to a Babel function, this could be caused by there being no suitable locale environment variables for the API you tried to use.",) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/data/venv/lib/python3.11/site-packages/redbot/__main__.py", line 470, in red_exception_handler red_task.result() File "/data/venv/lib/python3.11/site-packages/redbot/__main__.py", line 369, in run_bot await red.start(token) File "/data/venv/lib/python3.11/site-packages/redbot/core/bot.py", line 1278, in start await self._pre_login() File "/data/venv/lib/python3.11/site-packages/redbot/core/bot.py", line 1150, in _pre_login _i18n.set_global_regional_format(i18n_regional_format) File "/data/venv/lib/python3.11/site-packages/redbot/core/_i18n.py", line 60, in set_global_regional_format language_code = _get_standardized_locale_name(language_code) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/data/venv/lib/python3.11/site-packages/redbot/core/_i18n.py", line 42, in _get_standardized_locale_name raise ValueError("Invalid language code. Use format: `en-US`") ValueError: Invalid language code. Use format: `en-US` ``` with config such as: ``` ... "locale": "en-US", "regional_format": ""} ``` While an empty string is not something that could have been set by Core, a previously public i18n API for setting locale did not validate its input and as such, some users could have such values in their config files. ### Have the changes in this PR been tested? No
Flame442 (Migrated from github.com) requested changes 2025-02-06 00:45:34 +01:00
@ -1145,9 +1145,28 @@ class Red(
self.owner_ids.add(self._owner_id_overwrite)
Flame442 (Migrated from github.com) commented 2025-02-06 00:31:17 +01:00

I probably would like to see it print the old and incorrect value in the warning message for debugging purposes - We don't want a case in the future where this catch bugs out to confuse users if their language gets reset.

I probably would like to see it print the old and incorrect value in the warning message for debugging purposes - We don't want a case in the future where this catch bugs out to confuse users if their language gets reset.
Jackenmen (Migrated from github.com) reviewed 2025-02-06 01:01:02 +01:00
@ -1145,9 +1145,28 @@ class Red(
self.owner_ids.add(self._owner_id_overwrite)
Jackenmen (Migrated from github.com) commented 2025-02-06 01:01:02 +01:00

Updated.

Updated.
Flame442 (Migrated from github.com) approved these changes 2025-02-06 01:42:36 +01:00
Sign in to join this conversation.
No reviewers
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!6517
No description provided.