Skip to content

Commit

Permalink
Merge pull request #7537 from kozlovsky/fix/process_set_error
Browse files Browse the repository at this point in the history
Accept error values of any type in TriblerProcess.set_error
  • Loading branch information
kozlovsky authored Jul 10, 2023
2 parents 244b8de + fffe697 commit 7908cf8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
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

0 comments on commit 7908cf8

Please sign in to comment.