[RFC] We need a better error system
Created by: windmemory
Is your feature request related to a problem? Please describe. Recently we observed more and more limitation pushed out by WeChat. In some cases, connection to WeChat server got kicked out. It would be better that we emit these cases as errors so developers could handle them properly.
Currently all the error messages emitted is an Error
instance, only one message on it, so it is really hard to set a solid error type for the error. So here is this issue, I would like to bring this issue up and talk about the design for WechatyError
or something else :P
Describe the solution you'd like
We could have a parent error type as WechatyError
, then PuppetError
extends the parent, then each puppet has its own error class.
How about this design:
class WechatyError extends Error {
public readonly type: WechatyErrorType
public readonly message: string
constructor (
type: WechatyErrorType,
message: string,
) {
super()
this.type = type
this.message = message
}
}
Or we could make this parent error as an abstract class, then all the other errors extends this one.
Describe alternatives you've considered Haven't considered any alternatives...
Additional context
I've implemented one temporary solution in wechaty-puppet-padpro
, but that's just a test, you could check it here:
https://github.com/botorange/wechaty-puppet-padpro/commit/47d8de1bf885201629c8dd6dd34aaf721260d1c5
Here is a small piece of code that how I use it:
bot
.on('error', error => {
const errorMessage = error.message
const subMessages = errorMessage.split(' ')
if (subMessages[0] === 'PADPRO_ERROR' && subMessages[1] === 'LOGIN') {
console.log(`-----------------------------------------------------
AutoLogin Error:
type: ${subMessages[2]}
message: ${subMessages[3]}
------------------------------------------------------`)
}
})
.start()
To be honest, this is ugly, but just treat this as 【抛砖引玉】(don't know how to translate this :P)
Let's make wechaty
better and better.
[enhancement]