Skip to content

Commit bc9a0fb

Browse files
authored
Improve error message
1 parent 60df1d7 commit bc9a0fb

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Lib/zoneinfo/_common.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@ def load_tzdata(key):
1414
if path.is_dir():
1515
raise IsADirectoryError
1616
return path.open("rb")
17+
except UnicodeEncodeError:
18+
# This error occurs because of unsupported filesystem encoding which
19+
# depends on the locale, such as keys containing a surrogate character.
20+
ZoneInfoNotFoundError(f"key {key} contains non-UTF8 character(s)")
1721
except (ImportError, FileNotFoundError, UnicodeEncodeError, IsADirectoryError):
18-
# There are four types of exception that can be raised that all amount
19-
# to "we cannot find this key":
22+
# There are three other types of exception that can be raised that
23+
# all amount to "we cannot find this key":
2024
#
2125
# ImportError: If package_name doesn't exist (e.g. if tzdata is not
2226
# installed, or if there's an error in the folder name like
2327
# Amrica/New_York)
2428
# FileNotFoundError: If resource_name doesn't exist in the package
2529
# (e.g. Europe/Krasnoy)
26-
# UnicodeEncodeError: If package_name or resource_name are not UTF-8,
27-
# such as keys containing a surrogate character.
2830
# IsADirectoryError: If package_name without a resource_name specified.
2931
raise ZoneInfoNotFoundError(f"No time zone found with key {key}")
3032

0 commit comments

Comments
 (0)