Greetings!
I’m trying to add a BeforeAuth hook to validate the version of my client. I’ll pass the ClientVersion in the auth variables, and then use a hook to check if that matches the latest version.
If not, I want to return an error. The code samples for hooks show how to return an error. In lua, it’s just error(“This is my error message.”)
The problem I’m running into is that the actually error message in the NERROR object is extremely verbose including the path the module, the error message, and then the whole stack trace. Example, my hook returns error(“Auth Rejected.”)
and my client prints:
message: /nakama/data/modules/checkClientVersionBeforeAuth.lua:8: Auth Rejected.
stack traceback:
[G]: in function ‘error’
/nakama/data/modules/checkClientVersionBeforeAuth.lua:8: in main chunk
I could theoretically try to use substring to try to chop off some this stuff and just get to my actual error message, but that seems extremely flimsy. I will have to update the substring arguments in my error function in my client if I ever make any changes on the server such as change the name of the module? Painful.
What is the best way to return an error message from a Hook so that I can easily display that message to the client?
Thanks!
Hello @Mehoo462,
Have a look at this page: Lua Runtime - Heroic Labs Documentation, specifically the before-hook section.
Hope it helps.
Thanks! I Missed that section. I skipped straight to the code examples.
I made some updates. Now this:
`error({"Auth Rejected.", error_codes.CANCELED})`
Returns this:
Code = 0 message: Auth Rejected.
stack traceback:
[G]: in function ‘error’
/nakama/data/modules/checkClientVersionBeforeAuth.lua:28: in main chunk
[G]: ?
code: 1
The actually code of the NERROR is 0. The message, the traceback, and the code are all included in the NERROR message. The switch in my client that catches errors based on error code is not catching this as an Error 1. I am surely missing something.
Or is the expectation that Hooks will always return Error 0 and I need to Regex or something to parse details out of the NERROR message string?
Thanks for any assistance.
Okay, I think I may have been using an ancient version of the Nakama server. I created a new image so that I’m testing with the latest version.
The “stack traceback:” is not longer present in the message. This helps. Much easier to remove the unnecessary bits with substr() if they aren’t variable length. Unless i start returning error codes with more than one digit?!
So the behaviour seems strange to me? Is this the intenion? Or am I doing it wrong?
C++ Client

LUA function
RESULT:

Challenges with this:
-
The actual error code is 0. This makes the hook error code difficult to deal with by my switch that handles other error codes that might come back from the server.
-
The word “message:” is encoded in the message. I have to clip it with .substr(9). to just get the message. I think this is normal. Other errors from the server do the same.
-
the word code: and the actual code is appended to the message. This is a real pain. If I want to get the actual code of the error, I have to pull it out the string with subtsr(). Annoying, but possible I suppose. But I had better make sure not to use double or triple digit error codes. Otherwise I need to resort to some sort of regex magic for my client to be able to pull the message out of errors returned by my custom serverside functions?
Seems weird enough to make me wonder if I’m doing it wrong. Thanks for any input!