nakama-js icon indicating copy to clipboard operation
nakama-js copied to clipboard

Errors not decoded (unreadable)

Open Rojoss opened this issue 3 years ago • 0 comments

I'm trying to read errors sent by our backend (Golang) in the client but I'm unable to do so. The error.body is a ReadableStream which contains an Uint8Array. I have no clue how to decode these error messages and it's quite annoying not being able to see errors on the client. I asked on Gitter and was told to make an issue because this SDK should handle the decoding of errors.

I also noticed error.type is cors, not sure why that is?

Backend: github.com/heroiclabs/nakama-common v1.18.0 Frontend: "@heroiclabs/nakama-js": "^2.3.0"

This is the error object: image

Here is the code in case I'm just doing something wrong

backendSDK.sendRPC("ADD_BOT_MESSAGE", xxx).catch((err) =>
		{
			console.error(err);
			if (err.body)
			{
				const reader: ReadableStreamDefaultReader<unknown> = (err.body as ReadableStream).getReader();
				reader.read().then((res) =>
				{
					console.error(res);
				}).catch((error: unknown) =>
				{
					console.error(error);
				});
			}
		});
	public async sendRPC(id: string, msg: Uint8Array): Promise<Uint8Array>
	{
		const session: Session = await this.waitForConnection();
		return this._client.rpc(session, id, Networking.encode(msg)).then((response) =>
		{
			return Networking.decode(response.payload);
		});
	}

And on the backend I just return an error in RPC function

func AddBotMessageRPC(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
	userID, ok := ctx.Value(runtime.RUNTIME_CTX_USER_ID).(string)
	if !ok {
		return "", errors.New("invalid context, missing userID")
	}
	...
}

Rojoss avatar Nov 01 '21 15:11 Rojoss