Skip to content

Commit

Permalink
fix: handle possibly failing write
Browse files Browse the repository at this point in the history
Why was this not handled before? I do not know.
  • Loading branch information
Esinko committed Sep 4, 2024
1 parent 00e2c3a commit 80cb9ee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/service-workers/workers/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,15 @@ class Filesystem {
name: data.name ?? json.name,
date: data.date ?? new Date().getTime()
}

if (!base.name || base.name.trim().length === 0) {
reject(new Error("Invalid name provided!"))
break
}

reject("Write fail")
break

if (data.type === 0) {
// Files have data
base.data = data.data ?? json.data
Expand Down Expand Up @@ -342,8 +351,8 @@ class Filesystem {
throw new Error("Unknown filesystem type")
}
} catch (e) {
console.debug(e)
reject(new Error(`Failed to write: ${e.stack}`))
console.debug(new Error(`Failed to write: ${e.stack}`))
reject(e.toString())
}
})
}
Expand Down Expand Up @@ -482,10 +491,11 @@ com.onMessage.addEventListener("message", async (e) => {
console.error("No such filesystem instance")
break
}
const write = await instance.write(
instance.write(
e.content.id, e.content.write, e.content.location
)
com.send("callback", { id: e.id, write })
.then((write) => com.send("callback", { id: e.id, write }))
.catch((error) => com.send("callback", { id: e.id, error }))
break
}
case "callback": {
Expand Down
12 changes: 11 additions & 1 deletion src/utils/useActiveItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function useActiveItem(

const fsSave = useCallback(debounce((newData, targetId) => {
if (!newData) return
console.log("Saving ...")
window.internal.workers.api(
"Filesystem", "write", {
instance: window.internal.ui.activeFilesystemInstance,
Expand All @@ -24,7 +25,16 @@ export default function useActiveItem(
type: 0
}
}
).then(() => {
).then((msg) => {
if (msg.error) {
console.error("Failed to save:", msg.error)
console.debug("Target:", targetId)
console.debug("Data:", newData.data)
// eslint-disable-next-line no-alert
alert("Dokumentti on virheellinen ja sitä ei voitu tallentaa! Ota yhteys kehittäjiin ja lähetä heille loki: Kehittäjä -> Vie loki -osiosta!")
return
}

// Save success
window.internal.ui.saved = true
document.getElementById("saveIndicator").className = "savedIndicator"
Expand Down

0 comments on commit 80cb9ee

Please sign in to comment.