{"record":{"id":"7bd217cbb2243cef","repo":"decolua/9router","slug":"failed-to-import-database","errorCode":null,"errorMessage":"Failed to import database","messagePattern":"Failed to import database","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/app/(dashboard)/dashboard/profile/page.js","lineNumber":715,"sourceCode":"  };\n\n  const runImportDatabase = async (password) => {\n    const file = pendingImportRef.current;\n    if (!file) return;\n    setDbLoading(true);\n    try {\n      const raw = await file.text();\n      const payload = JSON.parse(raw);\n\n      const res = await fetch(\"/api/settings/database\", {\n        method: \"POST\",\n        headers: { \"Content-Type\": \"application/json\" },\n        body: JSON.stringify({ ...payload, password }),\n      });\n\n      const data = await res.json().catch(() => ({}));\n      if (!res.ok) {\n        throw new Error(data.error || \"Failed to import database\");\n      }\n\n      await reloadSettings();\n      setDbStatus({ type: \"success\", message: \"Database imported successfully\" });\n    } catch (err) {\n      setDbStatus({ type: \"error\", message: err.message || \"Invalid backup file\" });\n    } finally {\n      pendingImportRef.current = null;\n      setDbLoading(false);\n    }\n  };\n\n  // Confirm password modal, then run export or import.\n  const handleDbAuthConfirm = async () => {\n    const { mode, password } = dbAuth;\n    setDbAuth({ open: false, mode: \"\", password: \"\" });\n    if (mode === \"export\") await handleExportDatabase(password);\n    else if (mode === \"import\") await runImportDatabase(password);","sourceCodeStart":697,"sourceCodeEnd":733,"githubUrl":"https://github.com/decolua/9router/blob/90b52e06ffd666b7929554211474d01588f6b1f8/src/app/(dashboard)/dashboard/profile/page.js#L697-L733","documentation":"ProfilePage's database import handler throws this when POST /api/settings/database (backup payload + password) responds non-2xx without a specific error field. The surrounding catch also surfaces 'Invalid backup file' for local parse failures, so this message specifically indicates the server rejected the import.","triggerScenarios":"POST /api/settings/database with {…payload, password} returns non-2xx: wrong admin password (401), backup JSON structurally invalid for the server's importer (400), or a DB write/restore error (500); also thrown when the response body lacks data.error.","commonSituations":"Importing a backup from a newer/older schema version the current server can't restore; wrong admin password in the import dialog; backup file edited or truncated before upload.","solutions":["Check the network response status and body for the server's specific rejection reason","Verify the admin password is correct","Validate the backup JSON matches the expected export shape (keys the importer reads) before uploading","Confirm the backup's schema version is compatible with the running server; regenerate the backup from the current version if possible"],"exampleFix":"// before\nif (!res.ok) {\n  throw new Error(data.error || \"Failed to import database\");\n}\n// after\nif (!res.ok) {\n  throw new Error(data.error || `Failed to import database (HTTP ${res.status})`);\n}","handlingStrategy":"validation","validationCode":"// validate backup shape before upload\nconst payload = JSON.parse(fileText);\nif (!payload || typeof payload !== \"object\") throw new Error(\"Invalid backup file\");\nif (!password) throw new Error(\"Admin password required to import\");","typeGuard":"function isValidBackup(v) {\n  return v !== null && typeof v === \"object\" && !Array.isArray(v);\n}","tryCatchPattern":"try {\n  const res = await fetch(\"/api/settings/database\", { method: \"POST\", body: JSON.stringify({ ...payload, password }) });\n  const data = await res.json().catch(() => ({}));\n  if (!res.ok) throw new Error(data.error || `Failed to import database (HTTP ${res.status})`);\n} catch (err) {\n  setDbStatus({ type: \"error\", message: err.message || \"Invalid backup file\" });\n}","preventionTips":["Only import backups produced by the same (or compatible) server version","Validate the backup JSON structure client-side before POSTing","Confirm the admin password before starting the import","Keep a copy of the pre-import DB so a failed restore is recoverable"],"tags":["http","database","import","validation"],"backgroundTag":"http-request-failed","analyzedSha":"90b52e06ffd666b7929554211474d01588f6b1f8","analyzedAt":"2026-08-30T21:05:45.952Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}