Merge pull request #73 from ayntee/switch-case
refactor(gateway): use switch-case to avoid repetitive code
This commit is contained in:
commit
0e6814690d
1 changed files with 54 additions and 48 deletions
|
@ -177,55 +177,61 @@ export class Gateway extends EventEmitter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async onclose(event: CloseEvent): Promise<void> {
|
private async onclose({ reason, code }: CloseEvent): Promise<void> {
|
||||||
if (event.reason === RECONNECT_REASON) return
|
if (reason === RECONNECT_REASON) return
|
||||||
this.emit('close', event.code, event.reason)
|
this.emit('close', code, reason)
|
||||||
this.debug(`Connection Closed with code: ${event.code}`)
|
this.debug(`Connection Closed with code: ${code}`)
|
||||||
|
|
||||||
if (event.code === GatewayCloseCodes.UNKNOWN_ERROR) {
|
switch (code) {
|
||||||
this.debug('API has encountered Unknown Error. Reconnecting...')
|
case GatewayCloseCodes.UNKNOWN_ERROR:
|
||||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
this.debug('API has encountered Unknown Error. Reconnecting...')
|
||||||
this.reconnect()
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||||
} else if (event.code === GatewayCloseCodes.UNKNOWN_OPCODE) {
|
this.reconnect()
|
||||||
throw new Error("Unknown OP Code was sent. This shouldn't happen!")
|
break
|
||||||
} else if (event.code === GatewayCloseCodes.DECODE_ERROR) {
|
case GatewayCloseCodes.UNKNOWN_OPCODE:
|
||||||
throw new Error("Invalid Payload was sent. This shouldn't happen!")
|
throw new Error("Unknown OP Code was sent. This shouldn't happen!")
|
||||||
} else if (event.code === GatewayCloseCodes.NOT_AUTHENTICATED) {
|
case GatewayCloseCodes.DECODE_ERROR:
|
||||||
throw new Error('Not Authorized: Payload was sent before Identifying.')
|
throw new Error("Invalid Payload was sent. This shouldn't happen!")
|
||||||
} else if (event.code === GatewayCloseCodes.AUTHENTICATION_FAILED) {
|
case GatewayCloseCodes.NOT_AUTHENTICATED:
|
||||||
throw new Error('Invalid Token provided!')
|
throw new Error('Not Authorized: Payload was sent before Identifying.')
|
||||||
} else if (event.code === GatewayCloseCodes.INVALID_SEQ) {
|
case GatewayCloseCodes.AUTHENTICATION_FAILED:
|
||||||
this.debug('Invalid Seq was sent. Reconnecting.')
|
throw new Error('Invalid Token provided!')
|
||||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
case GatewayCloseCodes.INVALID_SEQ:
|
||||||
this.reconnect()
|
this.debug('Invalid Seq was sent. Reconnecting.')
|
||||||
} else if (event.code === GatewayCloseCodes.RATE_LIMITED) {
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||||
throw new Error("You're ratelimited. Calm down.")
|
this.reconnect()
|
||||||
} else if (event.code === GatewayCloseCodes.SESSION_TIMED_OUT) {
|
break
|
||||||
this.debug('Session Timeout. Reconnecting.')
|
case GatewayCloseCodes.RATE_LIMITED:
|
||||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
throw new Error("You're ratelimited. Calm down.")
|
||||||
this.reconnect(true)
|
case GatewayCloseCodes.SESSION_TIMED_OUT:
|
||||||
} else if (event.code === GatewayCloseCodes.INVALID_SHARD) {
|
this.debug('Session Timeout. Reconnecting.')
|
||||||
this.debug('Invalid Shard was sent. Reconnecting.')
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
this.reconnect(true)
|
||||||
this.reconnect()
|
break
|
||||||
} else if (event.code === GatewayCloseCodes.SHARDING_REQUIRED) {
|
case GatewayCloseCodes.INVALID_SHARD:
|
||||||
throw new Error("Couldn't connect. Sharding is required!")
|
this.debug('Invalid Shard was sent. Reconnecting.')
|
||||||
} else if (event.code === GatewayCloseCodes.INVALID_API_VERSION) {
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||||
throw new Error("Invalid API Version was used. This shouldn't happen!")
|
this.reconnect()
|
||||||
} else if (event.code === GatewayCloseCodes.INVALID_INTENTS) {
|
break
|
||||||
throw new Error('Invalid Intents')
|
case GatewayCloseCodes.SHARDING_REQUIRED:
|
||||||
} else if (event.code === GatewayCloseCodes.DISALLOWED_INTENTS) {
|
throw new Error("Couldn't connect. Sharding is required!")
|
||||||
throw new Error("Given Intents aren't allowed")
|
case GatewayCloseCodes.INVALID_API_VERSION:
|
||||||
} else {
|
throw new Error("Invalid API Version was used. This shouldn't happen!")
|
||||||
this.debug(
|
case GatewayCloseCodes.INVALID_INTENTS:
|
||||||
'Unknown Close code, probably connection error. Reconnecting in 5s.'
|
throw new Error('Invalid Intents')
|
||||||
)
|
case GatewayCloseCodes.DISALLOWED_INTENTS:
|
||||||
if (this.timedIdentify !== null) {
|
throw new Error("Given Intents aren't allowed")
|
||||||
clearTimeout(this.timedIdentify)
|
default:
|
||||||
this.debug('Timed Identify found. Cleared timeout.')
|
this.debug(
|
||||||
}
|
'Unknown Close code, probably connection error. Reconnecting in 5s.'
|
||||||
await delay(5000)
|
)
|
||||||
await this.reconnect(true)
|
if (this.timedIdentify !== null) {
|
||||||
|
clearTimeout(this.timedIdentify)
|
||||||
|
this.debug('Timed Identify found. Cleared timeout.')
|
||||||
|
}
|
||||||
|
await delay(5000)
|
||||||
|
await this.reconnect(true)
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue