Capture typeerror exception (#2052)

This commit is contained in:
Joakim Sørensen 2020-09-14 13:40:11 +02:00 committed by GitHub
parent 052a691a4d
commit c9db42583b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 5 deletions

View File

@ -1,15 +1,22 @@
"""Custom log messages."""
import logging
import re
import sentry_sdk
_LOGGER: logging.Logger = logging.getLogger(__name__)
RE_BIND_FAILED = re.compile(r".*Bind for.*:(\d*) failed: port is already allocated.*")
def format_message(message: str) -> str:
"""Return a formated message if it's known."""
match = RE_BIND_FAILED.match(message)
if match:
return (
f"Port '{match.group(1)}' is already in use by something else on the host."
)
try:
match = RE_BIND_FAILED.match(message)
if match:
return f"Port '{match.group(1)}' is already in use by something else on the host."
except TypeError as err:
_LOGGER.error("Type of message is not string - %s", err)
sentry_sdk.capture_exception(err)
return message

View File

@ -9,3 +9,9 @@ def test_format_message():
format_message(message)
== "Port '80' is already in use by something else on the host."
)
def test_exeption():
"""Tests the exception handling."""
message = b"byte"
assert format_message(message) == message