Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled Message JSON #98

Open
Omsad opened this issue Aug 24, 2021 · 0 comments
Open

Unhandled Message JSON #98

Omsad opened this issue Aug 24, 2021 · 0 comments

Comments

@Omsad
Copy link
Contributor

Omsad commented Aug 24, 2021

When processing the following json a null reference exception is thrown.

{
   "type":"MESSAGE",
   "data":{
      "topic":"chat_moderator_actions.80153562.80153562",
      "message":"{\"type\":\"channel_terms_action\",\"data\":{\"type\":\"add_permitted_term\",\"id\":\"REMOVED\",\"text\":\"crazy ur crazy\",\"requester_id\":\"REMOVED\",\"requester_login\":\"REMOVED\",\"channel_id\":\"REMOVED\",\"expires_at\":\"2021-08-19T06:13:40.169705384Z\",\"updated_at\":\"2021-08-19T05:13:40.169704026Z\",\"from_automod\":true}}"
   }
}

There aren't the created_by, created_by_user_id or target_user_id properties, which means a null reference exception is being thrown by the ChatModeratorActions constructor.

So I've changed the constructor of ChatModeratorActions to:

public ChatModeratorActions(string jsonStr)
{
    var json = JObject.Parse(jsonStr).SelectToken("data");
    Type = json.SelectToken("type")?.ToString();
    ModerationAction = json.SelectToken("moderation_action")?.ToString();
    if (json.SelectToken("args") != null)
        foreach (var arg in json.SelectToken("args"))
            Args.Add(arg.ToString());
    CreatedBy = json.SelectToken("created_by")?.ToString();
    CreatedByUserId = json.SelectToken("created_by_user_id")?.ToString();
    TargetUserId = json.SelectToken("target_user_id")?.ToString();
}

E.g. added ? to json.SelectToken("created_by") etc... The above will then parse but you have to modify the message case in the switch statement to handle that the ModerationAction is null, e.g. update:

case "chat_moderator_actions":
...
switch (cma?.ModerationAction.ToLower())

to

case "chat_moderator_actions":
...
switch (cma?.ModerationAction?.ToLower())

It will then go through the UnaccountedFor handler rather than throwing the exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant