How to parse Nakama Errors in go server framework?

I use the Go framework to create my desired functionaliy on the server side
But the problem I have is how can I parse the errors that return to the framework from the nakama side and take appropriate action based on the type of error.

err := nk.[someFunction]
err just have Error() function that returns only one string, which contains the text, explanation, error code, etc., which cannot be used.

how to parse errors and for example:
if err.ErrCode == PermissionDenied → do some logics
else if err.ErrCode == AlreadyExists → some other logics

You should be able to cast the error to a runtime.Error. This contains the Code and the Message.

err := nk.[SomeFunction]
var runErr *runtime.Error
isRunError := errors.As(err, &runErr)

I try this snippet but isRunError is always false and if I try to access runErr.Code it cause panic in server because of it’s empty.

Can you guide me further?

Try this instead:

runtimeErr, ok := err.(*runtime.Error)
if ok {
    fmt.Println("Runtime Error Code:", runtimeErr.Code)
} else {
    fmt.Println("Error:", err.Error())
}

Can I ask what specifically you are trying to do? Which nakama functions are you calling?

There was no difference with this new method.
I am currently trying to call nk.LinkGoogle and err.Error() is “rpc error: code = AlreadyExists desc = Google ID is already in use.”
But I don’t think there will be a change in the calling of other functions.

@sadegh-askari please exemplify what error are you looking for specifically and for what nk function?

Some runtime functions return the errors specified in https://github.com/heroiclabs/nakama-common/blob/master/runtime/runtime.go#L161-L221.

These errors are constant so you can just compare them with e.g (when applicable):
err == runtime.ErrStorageRejectedVersion