Catch PK API network errors
This commit is contained in:
		
							parent
							
								
									eadefef6a3
								
							
						
					
					
						commit
						a579b509d3
					
				
					 1 changed files with 10 additions and 5 deletions
				
			
		| 
						 | 
					@ -146,15 +146,20 @@ async function fetchMessage(messageID) {
 | 
				
			||||||
	// Their backend is weird. Sometimes it says "message not found" (code 20006) on the first try, so we make multiple attempts.
 | 
						// Their backend is weird. Sometimes it says "message not found" (code 20006) on the first try, so we make multiple attempts.
 | 
				
			||||||
	let attempts = 0
 | 
						let attempts = 0
 | 
				
			||||||
	do {
 | 
						do {
 | 
				
			||||||
		var res = await fetch(`https://api.pluralkit.me/v2/messages/${messageID}`)
 | 
							try {
 | 
				
			||||||
		if (res.ok) return res.json()
 | 
								var res = await fetch(`https://api.pluralkit.me/v2/messages/${messageID}`)
 | 
				
			||||||
 | 
								if (res.ok) return res.json()
 | 
				
			||||||
 | 
								var errorGetter = res.json
 | 
				
			||||||
 | 
							} catch (e) {
 | 
				
			||||||
 | 
								// Catch any network issues too.
 | 
				
			||||||
 | 
								errorGetter = e.toString
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// I think the backend needs some time to update.
 | 
							// I think the backend needs some time to update.
 | 
				
			||||||
		await new Promise(resolve => setTimeout(resolve, 2000))
 | 
							await new Promise(resolve => setTimeout(resolve, 1500))
 | 
				
			||||||
	} while (++attempts < 3)
 | 
						} while (++attempts < 3)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const errorMessage = await res.json()
 | 
						throw new Error(`PK API returned an error after ${attempts} tries: ${JSON.stringify(await errorGetter())}`)
 | 
				
			||||||
	throw new Error(`PK API returned an error after ${attempts} tries: ${JSON.stringify(errorMessage)}`)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports._memberToStateContent = memberToStateContent
 | 
					module.exports._memberToStateContent = memberToStateContent
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue