Some errors should be displayed to the user
This commit is contained in:
		
							parent
							
								
									8dc24a7763
								
							
						
					
					
						commit
						b812b8b17b
					
				
					 1 changed files with 5 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -15,13 +15,15 @@ const loadTags = sync.require("./load-tags")
 | 
			
		|||
/** @type {import("./schema")} */
 | 
			
		||||
const schema = sync.require("./schema")
 | 
			
		||||
 | 
			
		||||
class UserVisibleError extends Error {}
 | 
			
		||||
 | 
			
		||||
async function loadCollection(inputUsername) {
 | 
			
		||||
	assert.match(inputUsername, /^[a-z0-9_-]+$/)
 | 
			
		||||
	const html = await fetch(`https://bandcamp.com/${inputUsername}`).then(res => res.text())
 | 
			
		||||
	const doc = domino.createDocument(html)
 | 
			
		||||
 | 
			
		||||
	const first = doc.querySelector(".collection-item-container[data-token]")
 | 
			
		||||
	if (!first) throw new Error("Account does not exist. Please check the username - it's easy to mix it up with the display name, which is different.")
 | 
			
		||||
	if (!first) throw new UserVisibleError("Account does not exist. Please check the username - it's easy to mix it up with the display name, which is different.")
 | 
			
		||||
	assert(first)
 | 
			
		||||
	const token = first.getAttribute("data-token")
 | 
			
		||||
	assert(token)
 | 
			
		||||
| 
						 | 
				
			
			@ -110,7 +112,8 @@ router.post("/api/load-collection", defineEventHandler(async event => {
 | 
			
		|||
		setCookie(event, "accounts", (getCookie(event, "accounts") || "").split("|").concat(account).join("|"))
 | 
			
		||||
		return pugSync.render(event, "collection-loaded.pug", result)
 | 
			
		||||
	} catch (cause) {
 | 
			
		||||
		const error = new class extends Error {
 | 
			
		||||
		const error = cause instanceof UserVisibleError ? cause
 | 
			
		||||
		: new class extends Error {
 | 
			
		||||
			name = "Collection Import Error"
 | 
			
		||||
			account = account
 | 
			
		||||
			cause = cause
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue