diff --git a/package.json b/package.json index b3ec251..4bf01ac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "miakapi", - "version": "4.5.0", + "version": "4.5.1", "description": "Typed coordinator and trusted-browser SDK for Miakapp", "type": "module", "packageManager": "bun@1.2.23", diff --git a/src/internal/browser-errors.ts b/src/internal/browser-errors.ts index 747f43c..31db4fb 100644 --- a/src/internal/browser-errors.ts +++ b/src/internal/browser-errors.ts @@ -86,11 +86,14 @@ export function browserRelayFailure( retryable: boolean, outcome: DispatchOutcome, correlation?: FailureOptions['correlation'], + message?: unknown, ): BrowserClientError { const options: FailureOptions = correlation === undefined ? { code, retryable } : { code, retryable, correlation }; - return new BrowserClientError(kindFromCode(code), outcome, 'Relay rejected the operation', options); + // A coordinator's own refusal (2000-2999) keeps its sentence: the app shows it to the resident. + const application = code >= 2_000 && code <= 2_999 && typeof message === 'string' && message.length > 0 && message.length <= 256; + return new BrowserClientError(kindFromCode(code), outcome, application ? message : 'Relay rejected the operation', options); } export function safeBrowserLog( diff --git a/src/internal/user-calls.ts b/src/internal/user-calls.ts index 763a9b7..ab40125 100644 --- a/src/internal/user-calls.ts +++ b/src/internal/user-calls.ts @@ -220,7 +220,7 @@ export class UserCallManager implements BrowserCalls { : call.wasAccepted ? 'accepted' : 'not_dispatched'; const failure = browserRelayFailure(code, retryable, outcome, { kind: 'call', localId: call.localId, - }); + }, frame.payload[3]); this.#fail(call, failure); this.#host.emitFailure(failure); return true; diff --git a/test/browser-state-calls.test.ts b/test/browser-state-calls.test.ts index 01a2623..75e33a3 100644 --- a/test/browser-state-calls.test.ts +++ b/test/browser-state-calls.test.ts @@ -230,6 +230,20 @@ describe('browser state and calls', () => { await harness.client.stop(); }); + test('keeps a coordinator\'s own refusal sentence, never the relay\'s', async () => { + const harness = createBrowserTestHarness(); + const { connection } = await startBrowserReady(harness); + const refused = harness.client.calls.start({ function: 'home.echo', arguments: null, timeoutMs: 5_000 }); + await connection.nextClientFrame(Opcode.Call); + connection.send({ opcode: Opcode.CallError, payload: [1, 2605, false, 'Ce lien n’est pas valable', null] }); + expect(await refused.result.catch((error: unknown) => error)).toMatchObject({ code: 2605, message: 'Ce lien n’est pas valable' }); + const relay = harness.client.calls.start({ function: 'home.echo', arguments: null, timeoutMs: 5_000 }); + await connection.nextClientFrame(Opcode.Call); + connection.send({ opcode: Opcode.CallError, payload: [2, 1200, false, 'internal detail', null] }); + expect(await relay.result.catch((error: unknown) => error)).toMatchObject({ code: 1200, message: 'Relay rejected the operation' }); + await harness.client.stop(); + }); + test('ignores a correlated terminal frame after a local deadline settles the call', async () => { const harness = createBrowserTestHarness(); const failures: Array<{ kind: string }> = []; diff --git a/test/direct-node.test.mts b/test/direct-node.test.mts index e99d780..d43e2ac 100644 --- a/test/direct-node.test.mts +++ b/test/direct-node.test.mts @@ -74,7 +74,7 @@ test('a resident uses the home through the direct transport without the internet assert.equal(lamp, true); const refused = client.calls.start({ function: 'lamp.set', arguments: { on: 'yes' }, timeoutMs: 5_000 }); - await assert.rejects(refused.result, (error: { code?: unknown }) => error.code === 2001); + await assert.rejects(refused.result, (error: { code?: unknown; message?: unknown }) => error.code === 2001 && error.message === 'on must be a boolean'); // The ticket was renewed over the direct session itself. await waitFor(async () => (saved !== record ? saved : undefined));