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

Accept error values of any type in TriblerProcess.set_error #7537

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/tribler/core/utilities/process_manager/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import time
from datetime import datetime, timedelta
from enum import Enum
from typing import List, Optional, TYPE_CHECKING, Union
from typing import Any, List, Optional, TYPE_CHECKING

import psutil

Expand Down Expand Up @@ -183,10 +183,15 @@ def set_api_port(self, api_port: int):
self.api_port = api_port
self.save()

def set_error(self, error: Union[str | Exception], replace: bool = False):
if isinstance(error, Exception):
def set_error(self, error: Any, replace: bool = False):
# It is expected for `error` to be str or an instance of exception, but values of other types
# are handled gracefully as well: everything except None is converted to str
if error is not None and not isinstance(error, str):
error = f"{error.__class__.__name__}: {error}"
self.error_msg = error if replace else (self.error_msg or error)

if replace or not self.error_msg:
self.error_msg = error

self.save()

def finish(self, exit_code: Optional[int] = None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ def test_tribler_process_set_error(current_process):
r"started='[^']+', duration='\d:\d{2}:\d{2}', error='ValueError: exception text'\)$"
assert re.match(pattern, str(current_process))

# If the value of another type is specified, the method converts it to str
current_process.set_error({1: 2}, replace=True)
assert current_process.error_msg == 'dict: {1: 2}'

# None is not converted to str
current_process.set_error(None, replace=True)
assert current_process.error_msg is None


def test_tribler_process_mark_finished(current_process):
p = current_process # for brevity
Expand Down
Loading