Skip to content

Commit 7a89db4

Browse files
authored
fix: added exception handling for Xlib.error.BadWindow (#97)
Implemented a try/except block to catch the `Xlib.error.BadWindow` error during window name and class fetching. This change prevents the appearance of unnecessary messages in the logs, which although they do not affect the performance or operation of the watcher, they generate noise and do not provide relevant information about the error. With this enhancement, in case this error is detected, the window name or class will be set to "unknown", allowing the process to continue without interruption
1 parent 76c85c6 commit 7a89db4

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

aw_watcher_window/xlib.py

+26-9
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,24 @@ def get_window_name(window: Window) -> str:
7373
# But I don't know, so I pass the thing on, for now.
7474
d = None
7575
if d is None or d.format != 8:
76-
# Fallback.
77-
r = window.get_wm_name()
78-
if isinstance(r, str):
79-
return r
80-
else:
81-
logger.warning(
82-
"I don't think this case will ever happen, but not sure so leaving this message here just in case."
83-
)
84-
return r.decode("latin1") # WM_NAME with type=STRING.
76+
try:
77+
# Fallback.
78+
r = window.get_wm_name()
79+
if isinstance(r, str):
80+
return r
81+
else:
82+
logger.warning(
83+
"I don't think this case will ever happen, but not sure so leaving this message here just in case."
84+
)
85+
return r.decode("latin1") # WM_NAME with type=STRING.
86+
except Xlib.error.BadWindow as e:
87+
# I comment on the log, the number of messages that pop up is very annoying
88+
# Also, it does not give much information about the error
89+
# But I leave it in case someone sees it useful
90+
# logger.warning(
91+
# f"Unable to get window property WM_NAME, got a {type(e).__name__} exception from Xlib"
92+
# )
93+
return "unknown"
8594
else:
8695
# Fixing utf8 issue on Ubuntu (https://github.com/gurgeh/selfspy/issues/133)
8796
# Thanks to https://github.com/gurgeh/selfspy/issues/133#issuecomment-142943681
@@ -112,6 +121,14 @@ def get_window_class(window: Window) -> str:
112121
logger.warning("Code made an unclear branch")
113122
try:
114123
window = window.query_tree().parent
124+
except Xlib.error.BadWindow:
125+
# I comment on the log, the number of messages that pop up is very annoying
126+
# Also, it does not give much information about the error
127+
# But I leave it in case someone sees it useful
128+
# logger.warning(
129+
# "Unable to get window query_tree().parent, got a BadWindow exception."
130+
# )
131+
return "unknown"
115132
except Xlib.error.XError as e:
116133
logger.warning(
117134
f"Unable to get window query_tree().parent, got a {type(e).__name__} exception from Xlib"

0 commit comments

Comments
 (0)