Skip to content

Commit

Permalink
Handle unicode decode errors gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
L3337 committed Jan 25, 2024
1 parent c7680db commit 19c29f8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
22 changes: 22 additions & 0 deletions src/sglib/lib/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@
LANGUAGE = None
_ = lambda x: x

def log_decode(string: bytes, encoding=TEXT_ENCODING):
""" Attempt to decode a byte string with an unknown decoding to the system
encoding. If unable to decode, log as much information as possible
and re-raise the exception
"""
try:
return string.decode(encoding)
except Exception as ex:
try:
result = string.decode(
TEXT_ENCODING,
errors='replace',
)
except Exception as ex2:
result = str(ex2)
LOG.exception(
f"Unable to decode: {ex}, "
f"{locale.getlocale()}, {result}"
)
raise ex


def _gettext():
""" Deprecated. Probably will not use gettext when I implement translation
again, but keeping this just in case
Expand Down
37 changes: 29 additions & 8 deletions src/sgui/widgets/hardware_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
from sglib.lib.process import run_process
from sglib.math import clip_value
from sgui import sgqt

import ctypes
import locale
import os
import sys
import time
Expand All @@ -44,7 +46,7 @@
portmidi,
util,
)
from sglib.lib.translate import _, TEXT_ENCODING
from sglib.lib.translate import _, log_decode
from sglib.log import LOG

THREADS_TOOLTIP = _("""\
Expand Down Expand Up @@ -217,7 +219,10 @@ def check_device(self):

for i in range(f_count):
f_dev = self.pyaudio.Pa_GetDeviceInfo(i)
f_dev_name = f_dev.contents.name.decode(TEXT_ENCODING)
try:
f_dev_name = log_decode(f_dev.contents.name)
except:
continue
f_audio_device_names.append(f_dev_name)
if f_device_str == f_dev_name:
break
Expand Down Expand Up @@ -397,9 +402,13 @@ def f_close_event(a_self=None, a_event=None):
host_api_dict = {}

for i in range(f_count):
name = self.pyaudio.Pa_GetHostApiInfo(
host_api_info = self.pyaudio.Pa_GetHostApiInfo(
i,
).contents.name.decode(TEXT_ENCODING)
)
try:
name = log_decode(host_api_info.contents.name)
except:
continue
f_host_api_names.append(name)
host_api_dict[i] = name

Expand All @@ -412,7 +421,10 @@ def f_close_event(a_self=None, a_event=None):
LOG.info("Enumerating audio devices")
for i in range(f_count):
f_dev = self.pyaudio.Pa_GetDeviceInfo(i)
f_dev_name = f_dev.contents.name.decode(TEXT_ENCODING)
try:
f_dev_name = log_decode(f_dev.contents.name)
except:
continue
host_api_name = host_api_dict[f_dev.contents.hostApi]
LOG.info(
f"{i}: {host_api_name}: {f_dev_name} "
Expand Down Expand Up @@ -505,8 +517,12 @@ def out_count_changed(a_val):
LOG.info("Enumerating MIDI devices")
for loop in range(self.pypm.Pm_CountDevices()):
f_midi_device = self.pypm.Pm_GetDeviceInfo(loop)
f_midi_device_name = \
f_midi_device.contents.name.decode(TEXT_ENCODING)
try:
f_midi_device_name = log_decode(
f_midi_device.contents.name,
)
except:
continue
# LOG.info("DeviceID: {} Name: '{}' Input?: {} "
# "Output?: {} Opened: {} ".format(
# loop, f_midi_device_name, f_midi_device.contents.input,
Expand Down Expand Up @@ -692,7 +708,12 @@ def create_config(config_path):
)
LOG.info(f"Pa_IsFormatSupported returned {f_supported}")
if f_supported: # != 0
msg = self.pyaudio.Pa_GetErrorText(f_supported).decode()
try:
msg = log_decode(
self.pyaudio.Pa_GetErrorText(f_supported),
)
except Exception as ex:
msg = str(ex)
LOG.error(msg)
QMessageBox.warning(
f_window,
Expand Down

0 comments on commit 19c29f8

Please sign in to comment.