-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from ubclaunchpad/error_handling
Graceful Error Handling.
- Loading branch information
Showing
17 changed files
with
279 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import json | ||
import os | ||
|
||
""" | ||
Error Handler Class | ||
""" | ||
class ErrorHandler(): | ||
""" | ||
Sets up the directory where the languages are located | ||
""" | ||
def __init__(self, translations_dir): | ||
self.translations_dir = translations_dir | ||
|
||
""" | ||
Verify each language file is valid | ||
""" | ||
def verify_languages(self): | ||
""" | ||
key: language containing an error | ||
value: error message | ||
""" | ||
errors = {} | ||
|
||
all_language_files = os.listdir(self.translations_dir) | ||
|
||
for language_file in all_language_files: | ||
error = self.handle_error(language_file) | ||
if error != "": | ||
errors[language_file] = error | ||
|
||
return errors | ||
|
||
|
||
""" | ||
Used to handle an error from a language | ||
Input: string: language_file (source of error), boolean: error_expected (is the error expected?)\ | ||
Output: descriptive string about the error from the language file | ||
""" | ||
def handle_error(self, language_file, error_expected=False): | ||
invalid_file_result = "" | ||
# Verify if file is invalid | ||
invalid_file_result = self.handle_invalid_file(language_file) | ||
# print(invalid_file_result) | ||
if invalid_file_result != "": | ||
return invalid_file_result | ||
# Verify if any keys are invalid | ||
invalid_keys_result = self.handle_invalid_keys(language_file) | ||
if invalid_keys_result != "": | ||
return invalid_keys_result | ||
return "" | ||
|
||
""" | ||
Checks if given language is in an invalid file | ||
Output: | ||
- An empty string if there isn't any errors | ||
- A descriptive message about the error | ||
""" | ||
def handle_invalid_file(self, language_file): | ||
language_location = os.path.join(self.translations_dir, language_file) | ||
try: | ||
with open(language_location, "r") as file: | ||
json.load(file) | ||
except json.JSONDecodeError as e: | ||
return "Invalid Language File, try fixing the json format." | ||
except Exception as e: | ||
print(f"Unexpected Error: {e}") | ||
raise e | ||
# return empty string if no error found | ||
return "" | ||
|
||
""" | ||
Checks if given language contains any invalid keys | ||
Output: | ||
- An empty string if there aren't any errors | ||
- A descriptive message about the invalid key(s) | ||
""" | ||
def handle_invalid_keys(self, language_file): | ||
language_location = os.path.join(self.translations_dir, language_file) | ||
language = {} | ||
try: | ||
with open(language_location, "r") as file: | ||
language = json.load(file) | ||
for key in language: | ||
stripped_key = key.strip() | ||
if stripped_key == "": | ||
return "Key is empty or contains only whitespace." | ||
if not isinstance(language[key], str): | ||
return f"Value is not a string." | ||
except Exception as e: | ||
print(f"Unexpected Error: {e}") | ||
raise e | ||
return "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Metadata-Version: 2.1 | ||
Name: localization | ||
Version: 1.0.0 | ||
Summary: A localization package for microservices | ||
Author: UBC Launchpad | ||
Author-email: [email protected] | ||
Classifier: Programming Language :: Python :: 3 | ||
Classifier: License :: OSI Approved :: MIT License | ||
Classifier: Operating System :: OS Independent | ||
Requires-Python: >=3.6 | ||
Description-Content-Type: text/markdown | ||
License-File: LICENSE.txt | ||
|
||
### L10n | ||
A simple, lightweight interface for Python-based applications handle anything l10n/i18n | ||
MIT License | ||
|
||
Copyright (c) [2024] [UBC Launchpad] | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
LICENSE.txt | ||
README.md | ||
pyproject.toml | ||
setup.cfg | ||
src/internationalize/__init__.py | ||
src/internationalize/command_line.py | ||
src/internationalize/globals.py | ||
src/internationalize/helpers.py | ||
src/internationalize/internationalize.py | ||
src/internationalize/localize.py | ||
src/localization.egg-info/PKG-INFO | ||
src/localization.egg-info/SOURCES.txt | ||
src/localization.egg-info/dependency_links.txt | ||
src/localization.egg-info/top_level.txt | ||
tests/test.py | ||
tests/test_cli.py | ||
tests/test_generate_file.py | ||
tests/test_parse_json.py | ||
tests/test_read_file.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
internationalize |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"invalid_file.json": "Invalid Language File, try fixing the json format.", | ||
"empty_keys.json": "Key is empty or contains only whitespace.", | ||
"non_string_values.json": "Value is not a string.", | ||
"combined_issues.json": "Value is not a string." | ||
} |
5 changes: 5 additions & 0 deletions
5
i18nilize/tests/resources/error_handling/translations/combined_issues.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"Hi": 123, | ||
" ": "merci", | ||
"hello-world": "salut" | ||
} |
4 changes: 4 additions & 0 deletions
4
i18nilize/tests/resources/error_handling/translations/empty_keys.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"": "as", | ||
" ": "merci" | ||
} |
4 changes: 4 additions & 0 deletions
4
i18nilize/tests/resources/error_handling/translations/invalid_file.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"hello": "bonjour" | ||
"thanks": "merci" | ||
} |
4 changes: 4 additions & 0 deletions
4
i18nilize/tests/resources/error_handling/translations/non_string_values.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"Hi": 123, | ||
"Thanks": "merci" | ||
} |
4 changes: 4 additions & 0 deletions
4
i18nilize/tests/resources/error_handling/translations/valid_keys.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"hello": "bonjour", | ||
"thanks": "merci" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import json | ||
import os | ||
import unittest | ||
from src.internationalize.error_handler import ErrorHandler | ||
|
||
class TestErrorHandler(unittest.TestCase): | ||
def setUp(self): | ||
self.test_folder = os.path.join("tests", "resources", "error_handling") | ||
self.translations_folder = os.path.join(self.test_folder, "translations") | ||
self.handler = ErrorHandler(self.translations_folder) | ||
|
||
# ================== Test Keys ================================ | ||
def test_valid_keys(self): | ||
language = "valid_keys.json" | ||
self.assertEqual(self.handler.handle_invalid_keys(language), "") | ||
|
||
def test_non_string_values(self): | ||
language = "non_string_values.json" | ||
self.assertEqual(self.handler.handle_invalid_keys(language), "Value is not a string.") | ||
|
||
def test_empty_or_whitespace_keys(self): | ||
language = "empty_keys.json" | ||
self.assertEqual(self.handler.handle_invalid_keys(language), "Key is empty or contains only whitespace.") | ||
|
||
def test_combined_issues(self): | ||
language = "combined_issues.json" | ||
self.assertEqual(self.handler.handle_invalid_keys(language), "Value is not a string.") | ||
|
||
# ================== Test Invalid Files ================================ | ||
|
||
def test_invalid_file_individual(self): | ||
language = "invalid_file.json" | ||
self.assertEqual(self.handler.handle_invalid_file(language), "Invalid Language File, try fixing the json format.") | ||
|
||
# ================== Test Error Handler ================================ | ||
def test_invalid_file(self): | ||
print("invalid File Test: ") | ||
language = "invalid_file.json" | ||
self.assertEqual(self.handler.handle_error(language), "Invalid Language File, try fixing the json format.") | ||
|
||
def test_non_string_values(self): | ||
language = "non_string_values.json" | ||
self.assertEqual(self.handler.handle_error(language), "Value is not a string.") | ||
|
||
def test_valid_expected(self): | ||
language = "valid_keys.json" | ||
self.assertEqual(self.handler.handle_error(language), "") | ||
|
||
# ================== Test Folder ================================ | ||
def test_invalid_folder(self): | ||
with open(os.path.join(self.test_folder, "expected.json"), "r") as file: | ||
expected = json.load(file) | ||
|
||
self.assertEqual(self.handler.verify_languages(), expected) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"hello": "bonjour" | ||
"thanks": "merci" | ||
} |