feat(RESTManager): Better Errors
This commit is contained in:
		
							parent
							
								
									c2e690fe78
								
							
						
					
					
						commit
						87e1aad7e0
					
				
					 1 changed files with 20 additions and 24 deletions
				
			
		|  | @ -252,41 +252,36 @@ export class RESTManager { | ||||||
|     return rateLimited ? bucket : undefined |     return rateLimited ? bucket : undefined | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   handleStatusCode( |   async handleStatusCode( | ||||||
|     response: Response |     response: Response | ||||||
|   ): undefined | boolean { |   ): Promise<undefined> { | ||||||
|     const status = response.status |     const status = response.status | ||||||
| 
 | 
 | ||||||
|     if ( |     if ((status >= 200 && status < 400) || status === HttpResponseCode.TooManyRequests) return | ||||||
|       (status >= 200 && status < 400) || | 
 | ||||||
|       status === HttpResponseCode.TooManyRequests |     const body = await response.json() | ||||||
|     ) { |     const text = Deno.inspect(body.errors) | ||||||
|       return true |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     if (status === HttpResponseCode.Unauthorized) |     if (status === HttpResponseCode.Unauthorized) | ||||||
|       throw new Error('Request was not successful. Invalid Token.') |       throw new Error(`Request was not successful (Unauthorized). Invalid Token.\n${text}`) | ||||||
| 
 | 
 | ||||||
|     switch (status) { |     if ([ | ||||||
|       case HttpResponseCode.BadRequest: |       HttpResponseCode.BadRequest, | ||||||
|       case HttpResponseCode.Unauthorized: |       HttpResponseCode.NotFound, | ||||||
|       case HttpResponseCode.Forbidden: |       HttpResponseCode.Forbidden, | ||||||
|       case HttpResponseCode.NotFound: |       HttpResponseCode.MethodNotAllowed | ||||||
|       case HttpResponseCode.MethodNotAllowed: |     ].includes(status)) { | ||||||
|         throw new Error('Request Client Error.') |       throw new Error(`Request - Client Error. Code: ${status}\n${text}`) | ||||||
|       case HttpResponseCode.GatewayUnavailable: |     } else if (status === HttpResponseCode.GatewayUnavailable) { | ||||||
|         throw new Error('Request Server Error.') |       throw new Error(`Request - Server Error. Code: ${status}\n${text}`) | ||||||
|     } |     } else throw new Error('Request - Unknown Error') | ||||||
| 
 |  | ||||||
|     // left are all unknown
 |  | ||||||
|     throw new Error('Request Unknown Error') |  | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   async make( |   async make( | ||||||
|     method: RequestMethods, |     method: RequestMethods, | ||||||
|     url: string, |     url: string, | ||||||
|     body?: unknown, |     body?: unknown, | ||||||
|     retryCount = 0, |     maxRetries = 0, | ||||||
|     bucket?: string | null |     bucket?: string | null | ||||||
|   ): Promise<any> { |   ): Promise<any> { | ||||||
|     return await new Promise((resolve, reject) => { |     return await new Promise((resolve, reject) => { | ||||||
|  | @ -324,6 +319,7 @@ export class RESTManager { | ||||||
| 
 | 
 | ||||||
|           const response = await fetch(urlToUse, requestData) |           const response = await fetch(urlToUse, requestData) | ||||||
|           const bucketFromHeaders = this.processHeaders(url, response.headers) |           const bucketFromHeaders = this.processHeaders(url, response.headers) | ||||||
|  |           // eslint-disable-next-line @typescript-eslint/no-floating-promises
 | ||||||
|           this.handleStatusCode(response) |           this.handleStatusCode(response) | ||||||
| 
 | 
 | ||||||
|           if (response.status === 204) return resolve(undefined) |           if (response.status === 204) return resolve(undefined) | ||||||
|  | @ -333,7 +329,7 @@ export class RESTManager { | ||||||
|             json.retry_after !== undefined || |             json.retry_after !== undefined || | ||||||
|             json.message === 'You are being rate limited.' |             json.message === 'You are being rate limited.' | ||||||
|           ) { |           ) { | ||||||
|             if (retryCount > 10) { |             if (maxRetries > 10) { | ||||||
|               throw new Error('Max RateLimit Retries hit') |               throw new Error('Max RateLimit Retries hit') | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue