Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/save-preferences-result.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gemstack/framework': patch
---

Fix `savePreferences` rejecting the RPC when the underlying write fails. The telefunction advertises a `{ ok: false, error }` result and already returns it for the not-enabled case, but a failed disk write threw straight through, so the client saw a rejected call instead of the typed error. The write is now wrapped, so both failure modes return `{ ok: false }` and the client handles them the same way.
14 changes: 14 additions & 0 deletions packages/framework/src/dashboard-rpc/preferences.telefunc.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { strict as assert } from 'node:assert'
import { test } from 'node:test'
import { provideTelefuncContext } from 'telefunc'
import { onPreferences, savePreferences } from './preferences.telefunc.js'
import type { PreferencesStore } from '../registry.js'

// Outside a Telefunc `serve({ context })` there is no preferences store on the context — the
// same situation as the public relay, which never wires one. The RPCs must degrade safely: a
Expand All @@ -15,3 +17,15 @@ test('savePreferences with no store is a not-enabled no-op', async () => {
const result = await savePreferences({ autopilot: false })
assert.deepEqual(result, { ok: false, error: 'preferences are not enabled on this server' })
})

test('savePreferences returns the typed error when the store write fails, not a rejection', async () => {
const store: PreferencesStore = {
read: async () => ({}),
save: async () => {
throw new Error('disk full')
},
}
provideTelefuncContext({ preferences: store })
const result = await savePreferences({ autopilot: true })
assert.deepEqual(result, { ok: false, error: 'failed to save preferences' })
})
10 changes: 8 additions & 2 deletions packages/framework/src/dashboard-rpc/preferences.telefunc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export async function onPreferences(): Promise<Preferences> {
export async function savePreferences(preferences: Preferences): Promise<SavePreferencesResult> {
const store = contextPreferences()
if (!store) return { ok: false, error: 'preferences are not enabled on this server' }
await store.save(preferences)
return { ok: true }
// A failed write returns the advertised typed error rather than rejecting the RPC,
// so the client handles it the same as the not-enabled case (both `{ ok: false }`).
try {
await store.save(preferences)
return { ok: true }
} catch {
return { ok: false, error: 'failed to save preferences' }
}
}
Loading