From 0ff14c5c1af73b81f076adc97feb29da2d2a6e0c Mon Sep 17 00:00:00 2001 From: David Snopek Date: Fri, 11 Mar 2022 10:36:56 -0600 Subject: [PATCH] NakamaSocket: Emit errors received from the server on the "received_error" signal. (#94) Connection errors will be emitted on new "connection_error" signal. --- CHANGELOG.md | 6 +++ .../com.heroiclabs.nakama/api/NakamaRTAPI.gd | 52 +++++++++++++++++++ .../socket/NakamaSocket.gd | 18 ++++--- .../socket/NakamaSocketAdapter.gd | 2 +- 4 files changed, 71 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa71ded..6bd2b75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,12 @@ The format is based on [keep a changelog](http://keepachangelog.com/) and this p - Fix client errors parsing in Nakama 3.x - Make it possible to omit the label and query on NakamaClient.list_matches_async(). +### Backwards incompatible changes + +- The "received_error" signal on "NakamaSocket" is now emited with an "NakamaRTAPI.Error" object received from the server. + Previously, it was emitted with an integer error code when the socket failed to connect. + If you have old code using the "received_error" signal, you can switch to the new "connection_error" signal, which was added to replace it. + ## [2.1.0] - 2020-08-01 ### Added diff --git a/addons/com.heroiclabs.nakama/api/NakamaRTAPI.gd b/addons/com.heroiclabs.nakama/api/NakamaRTAPI.gd index 1860c8d..c38e76f 100644 --- a/addons/com.heroiclabs.nakama/api/NakamaRTAPI.gd +++ b/addons/com.heroiclabs.nakama/api/NakamaRTAPI.gd @@ -167,6 +167,58 @@ class ChannelPresenceEvent extends NakamaAsyncResult: return "channel_presence_event" +# Describes an error which occurred on the server. +class Error extends NakamaAsyncResult: + + const _SCHEMA = { + "code": {"name": "code", "type": TYPE_INT, "required": true}, + "message": {"name": "message", "type": TYPE_STRING, "required": true}, + "context": {"name": "context", "type": TYPE_DICTIONARY, "required": false, "content": TYPE_STRING}, + } + + # The selection of possible error codes. + enum Code { + # An unexpected result from the server. + RUNTIME_EXCEPTION = 0, + # The server received a message which is not recognised. + UNRECOGNIZED_PAYLOAD = 1, + # A message was expected but contains no content. + MISSING_PAYLOAD = 2, + # Fields in the message have an invalid format. + BAD_INPUT = 3, + # The match id was not found. + MATCH_NOT_FOUND = 4, + # The match join was rejected. + MATCH_JOIN_REJECTED = 5, + # The runtime function does not exist on the server. + RUNTIME_FUNCTION_NOT_FOUND = 6, + #The runtime function executed with an error. + RUNTIME_FUNCTION_EXCEPTION = 7, + } + + # The error code which should be one of "Error.Code" enums. + var code : int + + # A message in English to help developers debug the response. + var message : String + + # Additional error details which may be different for each response. + var context : Dictionary + + func _init(p_ex = null).(p_ex): + pass + + func _to_string(): + if is_exception(): return get_exception()._to_string() + return "Error" % [code, message, context] + + static func create(p_ns : GDScript, p_dict : Dictionary) -> Error: + return _safe_ret(NakamaSerializer.deserialize(p_ns, "Error", p_dict), Error) as Error + + static func get_result_key() -> String: + return "error" + + # A multiplayer match. class Match extends NakamaAsyncResult: diff --git a/addons/com.heroiclabs.nakama/socket/NakamaSocket.gd b/addons/com.heroiclabs.nakama/socket/NakamaSocket.gd index a7c9678..b2cab8f 100644 --- a/addons/com.heroiclabs.nakama/socket/NakamaSocket.gd +++ b/addons/com.heroiclabs.nakama/socket/NakamaSocket.gd @@ -11,14 +11,17 @@ signal closed() # Emitted when a socket is connected. signal connected() +# Emitted when an error occurs while connecting. +signal connection_error(p_error) + # Emitted when a chat channel message is received signal received_channel_message(p_channel_message) # ApiChannelMessage # Emitted when receiving a presence change for joins and leaves with users in a chat channel. signal received_channel_presence(p_channel_presence) # ChannelPresenceEvent -# Emitted when an error occurs on the socket. -signal received_error(p_error) +# Emitted when an error is received from the server. +signal received_error(p_error) # Error # Emitted when receiving a matchmaker matched message. signal received_matchmaker_matched(p_matchmaker_matched) # MatchmakerMatched @@ -98,7 +101,7 @@ func _init(p_adapter : NakamaSocketAdapter, _free_adapter = p_free_adapter _adapter.connect("closed", self, "_closed") _adapter.connect("connected", self, "_connected") - _adapter.connect("received_error", self, "_error") + _adapter.connect("received_error", self, "_connection_error") _adapter.connect("received", self, "_received") func _notification(what): @@ -124,8 +127,8 @@ func _closed(p_error = null): _resume_conn(ERR_CANT_CONNECT) _clear_responses() -func _error(p_error): - emit_signal("received_error", p_error) +func _connection_error(p_error): + emit_signal("connection_error", p_error) _resume_conn(p_error) _clear_responses() @@ -147,7 +150,10 @@ func _received(p_bytes : PoolByteArray): else: logger.error("Invalid call id received %s" % dict) else: - if dict.has("channel_message"): + if dict.has("error"): + var res = NakamaRTAPI.Error.create(NakamaRTAPI, dict["error"]) + emit_signal("received_error", res) + elif dict.has("channel_message"): var res = NakamaAPI.ApiChannelMessage.create(NakamaAPI, dict["channel_message"]) emit_signal("received_channel_message", res) elif dict.has("channel_presence_event"): diff --git a/addons/com.heroiclabs.nakama/socket/NakamaSocketAdapter.gd b/addons/com.heroiclabs.nakama/socket/NakamaSocketAdapter.gd index e9d7f3a..372a30a 100644 --- a/addons/com.heroiclabs.nakama/socket/NakamaSocketAdapter.gd +++ b/addons/com.heroiclabs.nakama/socket/NakamaSocketAdapter.gd @@ -15,7 +15,7 @@ signal connected() # A signal emitted when the socket is disconnected. signal closed() -# A signal emitted when the socket has an error when connected. +# A signal emitted when the socket has an error when connecting. signal received_error(p_exception) # A signal emitted when the socket receives a message.