Skip to content

Commit 8ab4fd1

Browse files
committed
fix: improve primitives fetching
1 parent 1f8943a commit 8ab4fd1

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

modules/hub/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ export default defineNuxtModule<ModuleOptions>({
108108
authorization: `Bearer ${hub.projectSecretKey || hub.userToken}`
109109
}
110110
})
111+
.catch((err) => {
112+
throw new Error(`Failed to fetch remote primitives: ${err?.data?.message || err.message}`)
113+
})
111114
logger.info(`Primitives available: ${Object.keys(primitives).filter(k => primitives[k]).map(k => `\`${k}\``).join(', ')} `)
112115
return
113116
} else {

server/api/_hub/primitives.get.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
export default eventHandler(async (event) => {
1+
export default eventHandler(async () => {
22
const [ dbCheck, kvCheck, blobCheck ] = await Promise.all([
3-
useDatabase().exec('PRAGMA table_list').catch(() => false),
4-
useKV().getKeys('__check__').catch(() => false),
5-
useBlob().list({ prefix: '__check__' }).catch(() => false)
3+
falseIfFail(() => useDatabase().exec('PRAGMA table_list')),
4+
falseIfFail(() => useKV().getKeys('__check__')),
5+
falseIfFail(() => useBlob().list({ prefix: '__check__' }))
66
])
77

88
return {
@@ -11,3 +11,15 @@ export default eventHandler(async (event) => {
1111
blob: Array.isArray(blobCheck),
1212
}
1313
})
14+
15+
async function falseIfFail (fn: () => any | Promise<any>) {
16+
try {
17+
const res = fn()
18+
if (res instanceof Promise) {
19+
return res.catch(() => false)
20+
}
21+
return res
22+
} catch (e) {
23+
return false
24+
}
25+
}

0 commit comments

Comments
 (0)