slackapi / node-slack-sdk

Slack Developer Kit for Node.js
https://tools.slack.dev/node-slack-sdk/
MIT License
3.28k stars 662 forks source link

different crash: how can I prevent slack-client from crashing all of my users? #153

Closed benzmuircroft closed 8 years ago

benzmuircroft commented 8 years ago

After my app created a channel

{ type: 'channel_created',
  channel: 
   { id: 'C0PJH9ZJS',
     is_channel: true,
     name: 'admin',
     created: 1456784697,
     creator: 'U0PB4E1NZ' },
  event_ts: '1456784697.353159' }

I always create channels the same way (with the same code) but for some reason this time my whole app crashed

rtm.on(RTM_EVENTS.CHANNEL_CREATED,created);//go do stuff with the channel
web.channels.create(U.username);

but It crashed (when normally on creating a channel it does not crash)

/var/sentora/hostdata/zadmin/public_html/node_modules/@slack/client/node_modules/eventemitter3/index.js:110
    case 2: listeners[i].fn.call(listeners[i].context, a1); break;
                           ^

TypeError: Cannot read property 'call' of null
at RTMClient.emit (/var/sentora/hostdata/zadmin/public_html/node_modules/@slack/client/node_modules/eventemitter3/index.js:110:32)
at RTMClient.emit (/var/sentora/hostdata/zadmin/public_html/node_modules/@slack/client/lib/clients/client.js:72:39)
at RTMClient._handleWsMessageViaEventHandler (/var/sentora/hostdata/zadmin/public_html/node_modules/@slack/client/lib/clients/rtm/client.js:425:8)
at RTMClient.handleWsMessage (/var/sentora/hostdata/zadmin/public_html/node_modules/@slack/client/lib/clients/rtm/client.js:373:10)
at WebSocket.wrapper (/var/sentora/hostdata/zadmin/public_html/node_modules/@slack/client/node_modules/lodash/index.js:3095:19)
at emitTwo (events.js:87:13)
at WebSocket.emit (events.js:172:7)
at Receiver.ontext (/var/sentora/hostdata/zadmin/public_html/node_modules/@slack/client/node_modules/ws/lib/WebSocket.js:816:10)
at /var/sentora/hostdata/zadmin/public_html/node_modules/@slack/client/node_modules/ws/lib/Receiver.js:477:18
at Receiver.applyExtensions (/var/sentora/hostdata/zadmin/public_html/node_modules/@slack/client/node_modules/ws/lib/Receiver.js:364:5)
benzmuircroft commented 8 years ago

I've made an attempt to contain your module under a node.js domain to try to catch and restart the slack-client on your errors (the errors seem very low level - within your node_modules)

ghost commented 8 years ago

What is the function you've registered as created in your code snippet?

rtm.on(RTM_EVENTS.CHANNEL_CREATED,created);//go do stuff with the channel
web.channels.create(U.username);

The crash you're seeing looks like it indicates that it's a null value. I'd check in your code to see whether it's getting nulled out anywhere.

benzmuircroft commented 8 years ago

LOL It is! I nulled it after as I wanted to stop listening after it's created!

function created(ch){
    console.log('created',ch);
    if(ch.channel.name==U.username){
        ⵌ.cid=ch.channel.id;
        ⵌ.rtm.on(ⵌ.RTM_EVENTS.CHANNEL_CREATED,null);//stop listening <<<<<-------- here
        G.SLACK[ⵌ.cid]={uid:U.id,'suid':s[1].uid};//saved a note of the connected user
        post();
        }
    return;
    }

How do I stop listening properly without causing this error?

ghost commented 8 years ago

You can look at the removeListener call on https://github.com/primus/eventemitter3