Skip to content

Commit

Permalink
NakamaSocket: Emit errors received from the server on the "received_e…
Browse files Browse the repository at this point in the history
…rror" signal. (#94)

Connection errors will be emitted on new "connection_error" signal.
  • Loading branch information
dsnopek authored Mar 11, 2022
1 parent 71fd065 commit 0ff14c5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions addons/com.heroiclabs.nakama/api/NakamaRTAPI.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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=%s, messages=%s, context=%s>" % [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:

Expand Down
18 changes: 12 additions & 6 deletions addons/com.heroiclabs.nakama/socket/NakamaSocket.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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()

Expand All @@ -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"):
Expand Down
2 changes: 1 addition & 1 deletion addons/com.heroiclabs.nakama/socket/NakamaSocketAdapter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 0ff14c5

Please sign in to comment.