Skip to content

Commit

Permalink
fix: alert if deserialization fails
Browse files Browse the repository at this point in the history
closes #256
  • Loading branch information
sxzz committed Jun 4, 2024
1 parent 10124d7 commit 071b1d1
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,14 @@ export function useStore(
const deserialize: ReplStore['deserialize'] = (serializedState: string) => {
if (serializedState.startsWith('#'))
serializedState = serializedState.slice(1)
const saved = JSON.parse(atou(serializedState))
let saved: any
try {
saved = JSON.parse(atou(serializedState))
} catch (err) {
console.error(err)
alert('Failed to load code from URL.')
return setDefaultFile()
}
for (const filename in saved) {
if (filename === '_version') {
vueVersion.value = saved[filename]
Expand Down Expand Up @@ -313,16 +320,19 @@ export function useStore(
applyBuiltinImportMap()
setActive(store.mainFile)
}

if (serializedState) {
deserialize(serializedState)
} else {
const setDefaultFile = (): void => {
setFile(
files.value,
mainFile.value,
template.value.welcomeSFC || welcomeSFCCode,
)
}

if (serializedState) {
deserialize(serializedState)
} else {
setDefaultFile()
}
if (!files.value[mainFile.value]) {
mainFile.value = Object.keys(files.value)[0]
}
Expand Down

0 comments on commit 071b1d1

Please sign in to comment.