Skip to content

Commit 16ff14d

Browse files
authored
Tolerate STOMP notification with empty body (#2088)
Details: * Fixed a JSONDecodeError when a STOMP notification from the HMC contained the empty string as the message body, by tolerating that. Signed-off-by: Andreas Maier <maiera@de.ibm.com>
1 parent c0c969a commit 16ff14d

3 files changed

Lines changed: 34 additions & 25 deletions

File tree

changes/2087.fix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed a JSONDecodeError when a STOMP notification from the HMC contained the
2+
empty string as the message body, by tolerating that.

zhmcclient/_notification.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,9 @@ def notifications(self):
627627
"Message format" in chapter 4. "Asynchronous notification" in the
628628
:term:`HMC API` book.
629629
630-
* message (:term:`JSON object`): Body of the HMC notification,
631-
converted into a JSON object. `None` for notifications that
632-
have no content in their response body.
630+
* message (:term:`JSON object` or `None`): Body of the HMC
631+
notification, converted into a JSON object. `None` for notifications
632+
that have no content in their response body.
633633
634634
The properties of the JSON object vary by notification type.
635635
@@ -688,13 +688,16 @@ def notifications(self):
688688

689689
# Now we have an item from the listener
690690
if item.msgtype == 'message':
691-
try:
692-
msg_obj = json.loads(item.message)
693-
except Exception as exc:
694-
raise NotificationParseError(
695-
"Cannot convert JMS message body to JSON: "
696-
f"{exc.__class__.__name__}: {exc}",
697-
item.message)
691+
if item.message is None:
692+
msg_obj = None
693+
else:
694+
try:
695+
msg_obj = json.loads(item.message)
696+
except Exception as exc:
697+
raise NotificationParseError(
698+
"Cannot convert JMS message body to JSON: "
699+
f"{exc.__class__.__name__}: {exc}",
700+
item.message)
698701
elif item.msgtype == 'error':
699702
if 'message' in item.headers:
700703
# Not sure that is always the case, but it was the case

zhmcclient/_utils.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -842,35 +842,39 @@ def get_headers_message(frame_args):
842842
Transform STOMP event method parameters to a tuple(headers, message),
843843
dependent on the stomp-py version that is used.
844844
845+
A message that is the empty string is returned as `None`.
846+
845847
Parameters:
846848
847849
frame_args: The STOMP frame, represented depending on the stomp-py
848850
package version as follows:
849851
850-
* if stomp-py uses Frames, a single stomp.Frame object:
852+
* On stomp.py >= 7.0.0, a stomp.Frame object with STOMP message
853+
headers and message body in attributes 'headers' and 'body'.
851854
852-
frame (stomp.Frame): Object with STOMP message headers and
853-
message body.
855+
* On stomp.py < 7.0.0, a tuple(headers, body).
854856
855-
* else, a tuple(headers, message):
857+
In both cases, the STOMP headers and body are represented as follows:
856858
857-
headers (dict): STOMP message headers.
858-
The headers are described in the `headers` tuple item
859-
returned by the
860-
:meth:`~zhmcclient.NotificationReceiver.notifications`
861-
method.
859+
headers (dict): STOMP message headers.
860+
The headers are described in the `headers` tuple item
861+
returned by the
862+
:meth:`~zhmcclient.NotificationReceiver.notifications`
863+
method.
862864
863-
message (string): STOMP message body as a string, which
864-
contains a serialized JSON object.
865-
The JSON object is described in the `message` tuple item
866-
returned by the
867-
:meth:`~zhmcclient.NotificationReceiver.notifications`
868-
method.
865+
message (str): STOMP message body as a Unicode string, which
866+
contains a serialized JSON object or the empty string.
867+
The JSON object is described in the `message` tuple item
868+
returned by the
869+
:meth:`~zhmcclient.NotificationReceiver.notifications`
870+
method.
869871
"""
870872
if len(frame_args) == 1:
871873
frame = frame_args[0]
872874
headers = frame.headers
873875
message = frame.body
874876
else:
875877
headers, message = frame_args
878+
if message == '':
879+
message = None
876880
return headers, message

0 commit comments

Comments
 (0)