wechaty / puppet-wechat

Wechaty Puppet Provider for WeChat
https://www.npmjs.com/package/wechaty-puppet-wechat
Apache License 2.0
419 stars 71 forks source link

fix request error when image sending too fast #185

Closed System233 closed 2 years ago

System233 commented 2 years ago

Finally I figured out where wx2.qq.comundefined come from: https://github.com/wechaty/puppet-wechat/blob/035e90461aa759b332056db9905bc5a9a4ac14e0/src/wechaty-bro.js#L332-L347 https://github.com/wechaty/webwx-app-tracker/blob/a12c78fb8bd7186c0f3bb0e18dd611151e6b8aac/formatted/webwxApp.js#L2100-L2106 some getMsgImg call returned undefined and FileBox requested the url.

If I send images one by one and save them, everything is fine, if I sending images too fast, an error will be occurred and crash the process when exception be passed to wechaty.

Error: getaddrinfo ENOTFOUND wx2.qq.comundefined
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26)

It should be fixed in #183, but I made a mistake, I thought $watch will works like Vue watch API, but it's not, the watch handler will be called immediately in first time even though MMStatus doesn't change.

183 should always fail in my manual tests, but it does work, and it should not happen in #184 tests(I think the error cause by the default value of MMStatus change to 'SENDING' in #183), which is weird.

Other Enhancements

  1. I want to increase the priority of file.mediaType to ensure that user behavior is not overwritten https://github.com/wechaty/puppet-wechat/blob/035e90461aa759b332056db9905bc5a9a4ac14e0/src/puppet-wechat.ts#L1245

    const contentType = file.mediaType || mime.getType(ext)  || undefined
  2. Can we add an error handler to catch exceptions from user listeners to ensure that process dose not crash?

    import {WechatyBuilder} from "wechaty"
    import {generate} from "qrcode-terminal"
    const bot=WechatyBuilder.build({puppet:'wechaty-puppet-wechat'});
    bot.on('scan',code=>generate(code,{small:true}));
    bot.on('message',()=>{
    throw new Error('crash')
    })
    bot.start();
huan commented 2 years ago

Can we add an error handler to catch exceptions from user listeners to ensure that the process does not crash?

If an error on wechaty instance has not been caught then the process will crash.

In order to catch it, we can use:

wechaty.on('error', e => {
  console.error(e)
})

to catch it.

Please let me know if you have more questions, thank you very much.

System233 commented 2 years ago

LGTM

Please feel free to help yourself to merge this PR when you believe everything is OK with it, thank you very much!

Thanks, I will.