# Registered rpc func can't return error message

**URL:** <https://forum.heroiclabs.com/t/registered-rpc-func-cant-return-error-message/387>\
**Category:** Game Design\
**Created:** [December 17, 2019, 9:39am UTC](https://forum.heroiclabs.com/t/registered-rpc-func-cant-return-error-message/387 "2019-12-17T09:39:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![opus\_kojiro](https://avatars.discourse-cdn.com/v4/letter/o/b487fb/32.png) [@opus\_kojiro](https://forum.heroiclabs.com/u/opus_kojiro)\
**Post date:** [December 17, 2019, 9:39am UTC](https://forum.heroiclabs.com/t/registered-rpc-func-cant-return-error-message/387/1 "2019-12-17T09:39:15Z")

</div>

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.Dictionary`2[System.String,System.Object]', GrpcStatusCode=13)`

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

---

<div class="post-metadata">

**Author:** ![novabyte](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/novabyte/32/1324_2.png) [@novabyte](https://forum.heroiclabs.com/u/novabyte)\
**Post date:** [December 17, 2019, 10:39am UTC](https://forum.heroiclabs.com/t/registered-rpc-func-cant-return-error-message/387/2 "2019-12-17T10:39:37Z")

</div>

Hey @opus_kojiro welcome 👋

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:

```go
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](https://grpc.io/) 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:

> <https://github.com/heroiclabs/nakama-unreal/blob/master/online_party_system/party_rpc.go>

---

<div class="post-metadata">

**Author:** ![opus\_kojiro](https://avatars.discourse-cdn.com/v4/letter/o/b487fb/32.png) [@opus\_kojiro](https://forum.heroiclabs.com/u/opus_kojiro)\
**Post date:** [December 17, 2019, 11:30am UTC](https://forum.heroiclabs.com/t/registered-rpc-func-cant-return-error-message/387/3 "2019-12-17T11:30:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![novabyte](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/novabyte/32/1324_2.png) [@novabyte](https://forum.heroiclabs.com/u/novabyte)\
**Post date:** [December 17, 2019, 6:22pm UTC](https://forum.heroiclabs.com/t/registered-rpc-func-cant-return-error-message/387/4 "2019-12-17T18:22:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![opus\_kojiro](https://avatars.discourse-cdn.com/v4/letter/o/b487fb/32.png) [@opus\_kojiro](https://forum.heroiclabs.com/u/opus_kojiro)\
**Post date:** [December 18, 2019, 1:00am UTC](https://forum.heroiclabs.com/t/registered-rpc-func-cant-return-error-message/387/5 "2019-12-18T01:00:25Z")

</div>

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