Skip to content

Commit 2f6530b

Browse files
fix: post merge improvement on table listener code and docstring (deephaven#5951)
Some defensive coding Code/docstring readability improvement --------- Co-authored-by: Chip Kent <5250374+chipkent@users.noreply.github.com>
1 parent 903abba commit 2f6530b

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

Integrations/src/main/java/io/deephaven/integrations/python/PythonMergedListenerAdapter.java

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ protected void propagateErrorDownstream(boolean fromProcess, @NotNull Throwable
116116
// and continue with the original exception.
117117
log.error().append("Python on_error callback failed: ").append(e2).endl();
118118
}
119+
} else {
120+
log.error().append("Python on_error callback is None: ").append(ExceptionUtils.getStackTrace(error)).endl();
119121
}
120122
}
121123
}

Integrations/src/main/java/io/deephaven/integrations/python/PythonReplayListenerAdapter.java

+3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ public void onFailureInternal(Throwable originalException, Entry sourceEntry) {
108108
// and continue with the original exception.
109109
log.error().append("Python on_error callback failed: ").append(e).endl();
110110
}
111+
} else {
112+
log.error().append("Python on_error callback is None: ")
113+
.append(ExceptionUtils.getStackTrace(originalException)).endl();
111114
}
112115
super.onFailureInternal(originalException, sourceEntry);
113116
}

py/server/deephaven/table_listener.py

+14-16
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
_JListenerRecorder = jpy.get_type("io.deephaven.engine.table.impl.ListenerRecorder")
2424
_JPythonMergedListenerAdapter = jpy.get_type("io.deephaven.integrations.python.PythonMergedListenerAdapter")
2525

26+
_DEFAULT_ON_ERROR_CALLBACK = lambda e : print(f"An error occurred during table update processing: {e}")
2627

2728
class TableUpdate(JObjectWrapper):
2829
"""A TableUpdate object represents a table update event. It contains the added, removed, and modified rows in the
@@ -202,12 +203,9 @@ def on_error(self, e: Exception) -> None:
202203
Args:
203204
e (Exception): the exception that occurred during the listener's execution.
204205
"""
205-
print(f"An error occurred during listener execution: {self}, {e}")
206+
print(f"An error occurred during table update processing: {self}, {e}")
206207

207208

208-
def _default_on_error(e: Exception) -> None:
209-
print(f"An error occurred during listener execution: {e}")
210-
211209
def _listener_wrapper(table: Table):
212210
"""A decorator to wrap a user listener function or on_update method to receive the numpy-converted Table updates.
213211
@@ -225,7 +223,6 @@ def wrapper(update, *args):
225223

226224
return decorator
227225

228-
229226
def _wrap_listener_func(t: Table, listener: Callable[[TableUpdate, bool], None]):
230227
n_params = len(signature(listener).parameters)
231228
if n_params != 2:
@@ -248,6 +245,7 @@ def wrapper(e):
248245

249246
return wrapper
250247

248+
251249
class TableListenerHandle(JObjectWrapper):
252250
"""A handle to manage a table listener's lifecycle."""
253251
j_object_type = _JPythonReplayListenerAdapter
@@ -287,12 +285,11 @@ def __init__(self, t: Table, listener: Union[Callable[[TableUpdate, bool], None]
287285
and then add the result tables as dependencies to the listener so that they can be safely read in it.
288286
on_error (Callable[[Exception], None]): a callback function to be invoked when an error occurs during the
289287
listener's execution. It should only be set when the listener is a function, not when it is an instance
290-
of TableListener. Defaults to None. When None, a default callback function will be provided that simply
288+
of TableListener. When the listener is a TableListener, TableListener.on_error will be used.
289+
Defaults to None. When None, a default callback function will be provided that simply
291290
prints out the received exception. If the callback function itself raises an exception, the new exception
292291
will be logged in the Deephaven server log and will not be further processed by the server.
293292
294-
295-
296293
Raises:
297294
DHError
298295
"""
@@ -313,7 +310,7 @@ def __init__(self, t: Table, listener: Union[Callable[[TableUpdate, bool], None]
313310
if on_error:
314311
on_error_callback = _error_callback_wrapper(on_error)
315312
else:
316-
on_error_callback = _error_callback_wrapper(_default_on_error)
313+
on_error_callback = _error_callback_wrapper(_DEFAULT_ON_ERROR_CALLBACK)
317314
else:
318315
raise DHError(message="listener is neither callable nor TableListener object")
319316

@@ -390,11 +387,11 @@ def listen(t: Table, listener: Union[Callable[[TableUpdate, bool], None], TableL
390387
and then add the result tables as dependencies to the listener so that they can be safely read in it.
391388
on_error (Callable[[Exception], None]): a callback function to be invoked when an error occurs during the
392389
listener's execution. It should only be set when the listener is a function, not when it is an instance
393-
of TableListener. Defaults to None. When None, a default callback function will be provided that simply
390+
of TableListener. When the listener is a TableListener, TableListener.on_error will be used.
391+
Defaults to None. When None, a default callback function will be provided that simply
394392
prints out the received exception. If the callback function itself raises an exception, the new exception
395393
will be logged in the Deephaven server log and will not be further processed by the server.
396394
397-
398395
Returns:
399396
a TableListenerHandle
400397
@@ -452,7 +449,7 @@ def on_error(self, e: Exception) -> None:
452449
Args:
453450
e (Exception): the exception that occurred during the listener's execution.
454451
"""
455-
print(f"An error occurred during listener execution: {self}, {e}")
452+
print(f"An error occurred during table update processing: {self}, {e}")
456453

457454

458455
class MergedListenerHandle(JObjectWrapper):
@@ -499,11 +496,11 @@ def __init__(self, tables: Sequence[Table], listener: Union[Callable[[Dict[Table
499496
and then add the result tables as dependencies to the listener so that they can be safely read in it.
500497
on_error (Callable[[Exception], None]): a callback function to be invoked when an error occurs during the
501498
listener's execution. It should only be set when the listener is a function, not when it is an instance
502-
of MergedListener. Defaults to None. When None, a default callback function will be provided that simply
499+
of MergedListener. When the listener is a MergedListener, MergedListener.on_error will be used.
500+
Defaults to None. When None, a default callback function will be provided that simply
503501
prints out the received exception. If the callback function itself raises an exception, the new exception
504502
will be logged in the Deephaven server log and will not be further processed by the server.
505503
506-
507504
Raises:
508505
DHError
509506
"""
@@ -525,7 +522,7 @@ def __init__(self, tables: Sequence[Table], listener: Union[Callable[[Dict[Table
525522
if on_error:
526523
on_error_callback = _error_callback_wrapper(on_error)
527524
else:
528-
on_error_callback = _error_callback_wrapper(_default_on_error)
525+
on_error_callback = _error_callback_wrapper(_DEFAULT_ON_ERROR_CALLBACK)
529526
else:
530527
raise DHError(message="listener is neither callable nor MergedListener object")
531528

@@ -625,7 +622,8 @@ def merged_listen(tables: Sequence[Table], listener: Union[Callable[[Dict[Table,
625622
and then add the result tables as dependencies to the listener so that they can be safely read in it.
626623
on_error (Callable[[Exception], None]): a callback function to be invoked when an error occurs during the
627624
listener's execution. It should only be set when the listener is a function, not when it is an instance
628-
of MergedListener. Defaults to None. When None, a default callback function will be provided that simply
625+
of MergedListener. When the listener is a MergedListener, MergedListener.on_error will be used.
626+
Defaults to None. When None, a default callback function will be provided that simply
629627
prints out the received exception. If the callback function itself raises an exception, the new exception
630628
will be logged in the Deephaven server log and will not be further processed by the server.
631629
"""

0 commit comments

Comments
 (0)