Registered rpc func can't return error message

environment: Unity + golang.

I want to handle a golang error as json string in exception message.
When I use Unity plugin method and RegisterBeforeXXX,

in golang:

func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, initializer runtime.Initializer) error {
	if err := initializer.RegisterBeforeAuthenticateDevice(beforeAuthenticateDevice); err != nil {
		logger.Error("OPUS : Unable to register: %v", err)
		return err
	}

	return nil
}

func beforeAuthenticateDevice(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, in *api.AuthenticateDeviceRequest) (*api.AuthenticateDeviceRequest, error) {
	return errors.New("hello error")
}

in Unity:
I can get the exception message correctly.

ApiResponseException(StatusCode=500, Message=‘hello error’, GrpcStatusCode=13)

But when I use rpc call,

in golang:

func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, initializer runtime.Initializer) error {
	if err := initializer.RegisterRpc("GetUserInfoRPC", getUserInfoRPC); err != nil {
		logger.Error("OPUS : Unable to register: %v", err)
		return err
	}

	return nil
}

func getUserInfoRPC(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
	return "", errors.New("hello error")
}

in Unity:
exception message is wrong like this.

ApiResponseException(StatusCode=500, Message='System.Collections.Generic.Dictionary2[System.String,System.Object]’, GrpcStatusCode=13)`

How do I get custom error message of golang error in rpc func?

Hey @opus_kojiro welcome :wave:

You can use the helper function runtime.NewError(string, int) to set your own error messages in responses.

In your example code it would look like:

func getUserInfoRPC(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
	// 3 = 400 Bad Request, 13 = 500 Internal Server Error, etc
	return "", runtime.NewError("hello error", 3)
}

The error numeric codes you pass into runtime.NewError are GRPC status codes rather than HTTP status codes but will be translated for you by the server runtime.

Have a look at the Go code for our implementation of the Online Party System for Unreal Engine with Nakama for more of the Go code patterns we use with Nakama:

Thank you for quick reply.

I tried this but got a same message.

return "", runtime.NewError("hello error", 3)

Unity C# get

ApiResponseException(StatusCode=400, Message=‘System.Collections.Generic.Dictionary`2[System.String,System.Object]’, GrpcStatusCode=13)

I tried on Nakama version 2.7.0 and 2.8.0.

@opus_kojiro What version of the client sdk do you use?

Now unity client sdk version is v2.3.1.
I tried v2.3.2 + nakama2.7.0 and nakama2.8.0 but same.