Skip to content

Instantly share code, notes, and snippets.

@shr0048
Last active February 10, 2023 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shr0048/e4cfceccb3b1752e6f5e37e9c34da8ae to your computer and use it in GitHub Desktop.
Save shr0048/e4cfceccb3b1752e6f5e37e9c34da8ae to your computer and use it in GitHub Desktop.
error_handling_ex
import time
import logger
class DataTransport:
retry_threshold: int = 5
retry_n_times: int = 5
def __init__(self, connector):
self._connector = connector
self.connection = None
def connect(self):
for _ in range(self.retry_n_times):
try:
self.connection = self._connector.connect()
except ConnectionError as e:
logger.info(
e, "Try To connect", self.retry_n_times
)
time.sleep(self.retry_threshold)
else:
return self.connection
raise ConnectionError(
f"{self.retry_n_times} times fail to connect"
)
def deliver_event(self, event):
try:
self.connect()
data = event.decode()
self.send(data)
# 문맥상 관계가 크게 없는 에러(ConnectionError, ValueError)
# 함수의 책임을 분산하기 위해서는 두 에러를 분리해야 함
except ConnectionError as e:
logger.info("Fail to connect", e)
raise
# event 의 decode 메서드에 속한 에러
except ValueError as e:
logger.info("Wrong data include", event, e)
raise
def send(self, data):
return self.connection.send(data)
import time
import logger
def connect_with_retry(connector, retry_n_times, retry_threshold: int = 5):
for _ in range(retry_n_times):
try:
return connector.connect()
except ConnectionError as e:
logger.info(
e, "Try To connect", retry_n_times
)
time.sleep(retry_threshold)
exc = ConnectionError(f"{retry_n_times} times fail to connect")
logger.info(exc)
raise exc
class DataTransport:
retry_threshold: int = 5
retry_n_times: int = 5
def __init__(self, connector):
self._connector = connector
self.connection = None
def deliver_event(self, event):
self.connection = connect_with_retry(self._connector, self.retry_n_times, self.retry_threshold)
self.send(event)
def send(self, event):
try:
return self.connection.send(event.decode())
except ValueError as e:
logger.info()
raise e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment