Some errors should be displayed to the user

This commit is contained in:
Cadence Ember 2025-04-17 14:57:16 +12:00
parent 8dc24a7763
commit b812b8b17b

View file

@ -15,13 +15,15 @@ const loadTags = sync.require("./load-tags")
/** @type {import("./schema")} */ /** @type {import("./schema")} */
const schema = sync.require("./schema") const schema = sync.require("./schema")
class UserVisibleError extends Error {}
async function loadCollection(inputUsername) { async function loadCollection(inputUsername) {
assert.match(inputUsername, /^[a-z0-9_-]+$/) assert.match(inputUsername, /^[a-z0-9_-]+$/)
const html = await fetch(`https://bandcamp.com/${inputUsername}`).then(res => res.text()) const html = await fetch(`https://bandcamp.com/${inputUsername}`).then(res => res.text())
const doc = domino.createDocument(html) const doc = domino.createDocument(html)
const first = doc.querySelector(".collection-item-container[data-token]") 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) assert(first)
const token = first.getAttribute("data-token") const token = first.getAttribute("data-token")
assert(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("|")) setCookie(event, "accounts", (getCookie(event, "accounts") || "").split("|").concat(account).join("|"))
return pugSync.render(event, "collection-loaded.pug", result) return pugSync.render(event, "collection-loaded.pug", result)
} catch (cause) { } catch (cause) {
const error = new class extends Error { const error = cause instanceof UserVisibleError ? cause
: new class extends Error {
name = "Collection Import Error" name = "Collection Import Error"
account = account account = account
cause = cause cause = cause