-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
1,776 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import os | ||
|
||
from tribler.core.utilities.exit_codes.tribler_exit_codes import exit_codes | ||
|
||
# pylint: disable=import-outside-toplevel | ||
|
||
|
||
check_win_errors = os.name == 'nt' | ||
|
||
|
||
def get_error_name(error_code: int) -> str: | ||
if error_code in exit_codes: | ||
return exit_codes[error_code] | ||
|
||
if check_win_errors: | ||
# Local import to avoid loading Windows error codes on non-Windows platforms. | ||
from tribler.core.utilities.exit_codes.win_error_codes import win_errors | ||
|
||
if error_code in win_errors: | ||
return win_errors[error_code].name | ||
|
||
try: | ||
return os.strerror(error_code) | ||
except ValueError: | ||
# On platforms where strerror() returns NULL when given an unknown error number, ValueError is raised. | ||
return 'Unknown error' |
11 changes: 11 additions & 0 deletions
11
src/tribler/core/utilities/exit_codes/tribler_exit_codes.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 |
---|---|---|
@@ -1,5 +1,16 @@ | ||
|
||
EXITCODE_OK = 0 # Normal exit code. | ||
EXITCODE_APPLICATION_ERROR = 1 # Generic error code for application errors. | ||
|
||
# Valid range for custom errors is 1..127 | ||
EXITCODE_DATABASE_IS_CORRUPTED = 99 # If the Core process finishes with this error, the GUI process restarts it. | ||
EXITCODE_ANOTHER_GUI_PROCESS_IS_RUNNING = 98 # A normal situation when a user double-clicks on the torrent file. | ||
EXITCODE_ANOTHER_CORE_PROCESS_IS_RUNNING = 97 # Should not happen if process locking is working correctly. | ||
|
||
|
||
exit_codes = {} | ||
|
||
|
||
for name, value in list(globals().items()): | ||
if name.startswith('EXITCODE_'): | ||
exit_codes[value] = name |
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,28 @@ | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
from typing import Dict | ||
|
||
|
||
win_error_codes_filename = Path(__file__).parent / 'win_error_codes.txt' | ||
|
||
|
||
@dataclass | ||
class ExitCode: | ||
code: int | ||
name: str | ||
description: str | ||
|
||
|
||
def parse_win_error_codes() -> Dict[int, ExitCode]: | ||
error_codes = {} | ||
|
||
with open(win_error_codes_filename, 'r', encoding='utf-8') as file: | ||
for line in file.readlines(): | ||
code, name, description = line.split(' ', 2) | ||
code = int(code) | ||
error_codes[code] = ExitCode(code, name, description.strip()) | ||
|
||
return error_codes | ||
|
||
|
||
win_errors = parse_win_error_codes() |
1,669 changes: 1,669 additions & 0 deletions
1,669
src/tribler/core/utilities/exit_codes/win_error_codes.txt
Large diffs are not rendered by default.
Oops, something went wrong.
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,18 @@ | ||
from unittest.mock import patch | ||
|
||
from tribler.core.utilities.exit_codes import get_error_name | ||
|
||
|
||
@patch('tribler.core.utilities.exit_codes.check_win_errors', True) | ||
def test_exit_codes(): | ||
assert get_error_name(0) == 'EXITCODE_OK' | ||
assert get_error_name(1) == 'EXITCODE_APPLICATION_ERROR' | ||
assert get_error_name(99) == 'EXITCODE_DATABASE_IS_CORRUPTED' | ||
assert get_error_name(98) == 'EXITCODE_ANOTHER_GUI_PROCESS_IS_RUNNING' | ||
assert get_error_name(97) == 'EXITCODE_ANOTHER_CORE_PROCESS_IS_RUNNING' | ||
|
||
assert get_error_name(-1073741819) == 'STATUS_ACCESS_VIOLATION' | ||
assert get_error_name(-1073740940) == 'STATUS_HEAP_CORRUPTION' | ||
|
||
with patch('os.strerror', side_effect=ValueError): | ||
assert get_error_name(1000000) == 'Unknown error' |
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