Skip to content

Commit 7038c0f

Browse files
committed
chore(on_data): Error handling for received message
1 parent a18a70f commit 7038c0f

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

examples/speech_to_text_v1.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ def on_inactivity_timeout(self, error):
5050
def on_listening(self):
5151
print('Service is listening')
5252

53-
def on_transcription_complete(self):
54-
print('Transcription completed')
55-
5653
def on_hypothesis(self, hypothesis):
5754
print(hypothesis)
5855

watson_developer_cloud/websocket/recognize_listener.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ def on_data(self, ws, message, message_type, fin):
143143
:param message_type: Message type which is either ABNF.OPCODE_TEXT or ABNF.OPCODE_BINARY
144144
:param fin: continue flag. If 0, the data continues.
145145
"""
146-
json_object = json.loads(message)
146+
147+
try:
148+
json_object = json.loads(message)
149+
except Exception:
150+
self.on_error(ws, 'Unable to parse received message.')
147151

148152
if 'error' in json_object:
149153
# Only call on_error() if a real error occurred. The STT service sends

0 commit comments

Comments
 (0)