Skip to content

Commit ce43d7e

Browse files
committed
catch errors on database fetch
1 parent 64f99d3 commit ce43d7e

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/runtime/plugins/db.client.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ export default defineNuxtPlugin(async nuxtApp => {
1111
fetchAt: null,
1212
async fetch() {
1313
this.fetchAt = Date.now()
14-
return dbUrl ? await fetch(dbUrl).then(res => res.json()) : []
14+
try {
15+
return await fetch(dbUrl).then(res => res.json())
16+
} catch (e) {
17+
console.error(e)
18+
return []
19+
}
1520
}
1621
}
1722

src/runtime/plugins/db.server.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ export default defineNuxtPlugin(async nuxtApp => {
1212
fetchAt: null,
1313
async fetch() {
1414
this.fetchAt = Date.now()
15-
return dbPath ? JSON.parse(await readFile(dbPath, 'utf8')) : []
15+
try {
16+
return JSON.parse(await readFile(dbPath, 'utf8'))
17+
} catch (e) {
18+
console.error(e)
19+
return []
20+
}
1621
}
1722
}
1823

0 commit comments

Comments
 (0)