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,46 +177,51 @@ 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) { | ||||||
|  |       case GatewayCloseCodes.UNKNOWN_ERROR: | ||||||
|         this.debug('API has encountered Unknown Error. Reconnecting...') |         this.debug('API has encountered Unknown Error. Reconnecting...') | ||||||
|         // eslint-disable-next-line @typescript-eslint/no-floating-promises
 |         // eslint-disable-next-line @typescript-eslint/no-floating-promises
 | ||||||
|         this.reconnect() |         this.reconnect() | ||||||
|     } else if (event.code === GatewayCloseCodes.UNKNOWN_OPCODE) { |         break | ||||||
|  |       case GatewayCloseCodes.UNKNOWN_OPCODE: | ||||||
|         throw new Error("Unknown OP Code was sent. This shouldn't happen!") |         throw new Error("Unknown OP Code was sent. This shouldn't happen!") | ||||||
|     } else if (event.code === GatewayCloseCodes.DECODE_ERROR) { |       case GatewayCloseCodes.DECODE_ERROR: | ||||||
|         throw new Error("Invalid Payload was sent. This shouldn't happen!") |         throw new Error("Invalid Payload was sent. This shouldn't happen!") | ||||||
|     } else if (event.code === GatewayCloseCodes.NOT_AUTHENTICATED) { |       case GatewayCloseCodes.NOT_AUTHENTICATED: | ||||||
|         throw new Error('Not Authorized: Payload was sent before Identifying.') |         throw new Error('Not Authorized: Payload was sent before Identifying.') | ||||||
|     } else if (event.code === GatewayCloseCodes.AUTHENTICATION_FAILED) { |       case GatewayCloseCodes.AUTHENTICATION_FAILED: | ||||||
|         throw new Error('Invalid Token provided!') |         throw new Error('Invalid Token provided!') | ||||||
|     } else if (event.code === GatewayCloseCodes.INVALID_SEQ) { |       case GatewayCloseCodes.INVALID_SEQ: | ||||||
|         this.debug('Invalid Seq was sent. Reconnecting.') |         this.debug('Invalid Seq was sent. Reconnecting.') | ||||||
|         // eslint-disable-next-line @typescript-eslint/no-floating-promises
 |         // eslint-disable-next-line @typescript-eslint/no-floating-promises
 | ||||||
|         this.reconnect() |         this.reconnect() | ||||||
|     } else if (event.code === GatewayCloseCodes.RATE_LIMITED) { |         break | ||||||
|  |       case GatewayCloseCodes.RATE_LIMITED: | ||||||
|         throw new Error("You're ratelimited. Calm down.") |         throw new Error("You're ratelimited. Calm down.") | ||||||
|     } else if (event.code === GatewayCloseCodes.SESSION_TIMED_OUT) { |       case GatewayCloseCodes.SESSION_TIMED_OUT: | ||||||
|         this.debug('Session Timeout. Reconnecting.') |         this.debug('Session Timeout. Reconnecting.') | ||||||
|         // eslint-disable-next-line @typescript-eslint/no-floating-promises
 |         // eslint-disable-next-line @typescript-eslint/no-floating-promises
 | ||||||
|         this.reconnect(true) |         this.reconnect(true) | ||||||
|     } else if (event.code === GatewayCloseCodes.INVALID_SHARD) { |         break | ||||||
|  |       case GatewayCloseCodes.INVALID_SHARD: | ||||||
|         this.debug('Invalid Shard was sent. 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() |         this.reconnect() | ||||||
|     } else if (event.code === GatewayCloseCodes.SHARDING_REQUIRED) { |         break | ||||||
|  |       case GatewayCloseCodes.SHARDING_REQUIRED: | ||||||
|         throw new Error("Couldn't connect. Sharding is required!") |         throw new Error("Couldn't connect. Sharding is required!") | ||||||
|     } else if (event.code === GatewayCloseCodes.INVALID_API_VERSION) { |       case GatewayCloseCodes.INVALID_API_VERSION: | ||||||
|         throw new Error("Invalid API Version was used. This shouldn't happen!") |         throw new Error("Invalid API Version was used. This shouldn't happen!") | ||||||
|     } else if (event.code === GatewayCloseCodes.INVALID_INTENTS) { |       case GatewayCloseCodes.INVALID_INTENTS: | ||||||
|         throw new Error('Invalid Intents') |         throw new Error('Invalid Intents') | ||||||
|     } else if (event.code === GatewayCloseCodes.DISALLOWED_INTENTS) { |       case GatewayCloseCodes.DISALLOWED_INTENTS: | ||||||
|         throw new Error("Given Intents aren't allowed") |         throw new Error("Given Intents aren't allowed") | ||||||
|     } else { |       default: | ||||||
|         this.debug( |         this.debug( | ||||||
|           'Unknown Close code, probably connection error. Reconnecting in 5s.' |           'Unknown Close code, probably connection error. Reconnecting in 5s.' | ||||||
|         ) |         ) | ||||||
|  | @ -226,6 +231,7 @@ export class Gateway extends EventEmitter { | ||||||
|         } |         } | ||||||
|         await delay(5000) |         await delay(5000) | ||||||
|         await this.reconnect(true) |         await this.reconnect(true) | ||||||
|  |         break | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue