# Hook Response Code/Message

**URL:** <https://forum.heroiclabs.com/t/hook-response-code-message/2770>\
**Category:** Runtime Framework\
**Created:** [June 22, 2022, 12:58pm UTC](https://forum.heroiclabs.com/t/hook-response-code-message/2770 "2022-06-22T12:58:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexandreK38](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/alexandrek38/32/1166_2.png) [@AlexandreK38](https://forum.heroiclabs.com/u/AlexandreK38)\
**Post date:** [June 22, 2022, 12:58pm UTC](https://forum.heroiclabs.com/t/hook-response-code-message/2770/1 "2022-06-22T12:58:11Z")

</div>

Hi!  
I’m trying to add a hook to prevent given usernames to be used, registered on `UpdateAccount` function. According to Server doc we need to return `nil` to halt the process. By doing this, the client sees the error code as `"NotFound"` and message `"The resource could not be found"`. I guess because putting `nil` prevent the UpdateAccount function to find the user and trigger this, but on the client we can’t have an error code to explicitely know if was our hook that halted the process. The client error code is an enum so I guess we could not add new values.

Then I thought that instead of returning `nil` I raise an error doing `error("Username forbidden")` and then the client receive `"InternalError"` as error code and `"Username forbidden"` as message. That way I could know with the message that it’s my code which triggered the error.

Is this a good solution? Althought it’s not what the doc tells to do.  
Thanks,  
Alexandre

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [June 22, 2022, 1:41pm UTC](https://forum.heroiclabs.com/t/hook-response-code-message/2770/2 "2022-06-22T13:41:33Z")

</div>

Hello @AlexandreK38, could you please share what runtime you are using is and what your hook code looks like?

---

<div class="post-metadata">

**Author:** ![AlexandreK38](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/alexandrek38/32/1166_2.png) [@AlexandreK38](https://forum.heroiclabs.com/u/AlexandreK38)\
**Post date:** [June 22, 2022, 1:47pm UTC](https://forum.heroiclabs.com/t/hook-response-code-message/2770/3 "2022-06-22T13:47:58Z")

</div>

Hi sesposito;  
I’m running Nakama 3.6 locally and client is latest Nakama Cocos2d-x C++ client (2.5.1)

Here is the Lua code

```auto
local function filter_username(context, json)
  if json.username == "toto" then
    error("Username forbidden")
    -- or
    -- return nil
  else
    return json
  end
end

nk.register_req_before(filter_username, "UpdateAccount")

```

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [June 22, 2022, 2:06pm UTC](https://forum.heroiclabs.com/t/hook-response-code-message/2770/4 "2022-06-22T14:06:30Z")

</div>

Please take a look at [Custom rpc response status in lua - #2 by zyro](https://forum.heroiclabs.com/t/custom-rpc-response-status-in-lua/619/2), it should cover what you’re looking for.

Cheers

---

<div class="post-metadata">

**Author:** ![AlexandreK38](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/alexandrek38/32/1166_2.png) [@AlexandreK38](https://forum.heroiclabs.com/u/AlexandreK38)\
**Post date:** [June 22, 2022, 2:10pm UTC](https://forum.heroiclabs.com/t/hook-response-code-message/2770/5 "2022-06-22T14:10:42Z")

</div>

Thanks for the answer,  
But as Zyro said in his post, the error code is restricted to a given list

> Where the code must be a GRPC response code numeric value from [this list](https://github.com/grpc/grpc/blob/master/doc/statuscodes.md)

So you can’t give your own error code, the only way to know it’s your error is the error message I guess.

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [June 22, 2022, 2:16pm UTC](https://forum.heroiclabs.com/t/hook-response-code-message/2770/6 "2022-06-22T14:16:34Z")

</div>

Those gRPC codes map to HTTP status codes as listed here: [googleapis/code.proto at master · googleapis/googleapis · GitHub](https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto). I’d suggest you use code 3, which maps to an http 400 status.

---

<div class="post-metadata">

**Author:** ![AlexandreK38](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/alexandrek38/32/1166_2.png) [@AlexandreK38](https://forum.heroiclabs.com/u/AlexandreK38)\
**Post date:** [June 22, 2022, 2:18pm UTC](https://forum.heroiclabs.com/t/hook-response-code-message/2770/7 "2022-06-22T14:18:37Z")

</div>

Yes that seems to be better than the InternalError (500) if no code is given, thanks 🙂
