From bdb88507df6c97f880ba7ee9fc7a0af1bc298719 Mon Sep 17 00:00:00 2001 From: Reynaldi Chernando <12949382+reynaldichernando@users.noreply.github.com> Date: Fri, 13 Nov 2026 10:47:03 +0700 Subject: [PATCH] throw error for get usage APIs (#4608) --- src/puter-js/src/modules/Auth.js | 27 ++++++++++++++ src/puter-js/tests/api/suites/auth.suite.ts | 41 ++++++++++++++++++--- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/puter-js/src/modules/Auth.js b/src/puter-js/src/modules/Auth.js index 9c89948b5..080da7fcd 100644 --- a/src/puter-js/src/modules/Auth.js +++ b/src/puter-js/src/modules/Auth.js @@ -789,6 +789,15 @@ export class AuthModule extends PuterModule { includePuterAuth: true, logContext: { service: 'auth', operation: 'usage', params: {} }, }); + if (!resp.ok) { + const body = await resp.json().catch(() => ({})); + throw { + status: resp.status, + message: body?.message ?? body?.error + ?? `getMonthlyUsage failed with status ${resp.status}`, + ...(body?.code ? { code: body.code } : {}), + }; + } return await resp.json(); } @@ -815,6 +824,15 @@ export class AuthModule extends PuterModule { }, }, ); + if (!resp.ok) { + const body = await resp.json().catch(() => ({})); + throw { + status: resp.status, + message: body?.message ?? body?.error + ?? `getDetailedAppUsage failed with status ${resp.status}`, + ...(body?.code ? { code: body.code } : {}), + }; + } return await resp.json(); } @@ -835,6 +853,15 @@ export class AuthModule extends PuterModule { params: {}, }, }); + if (!resp.ok) { + const body = await resp.json().catch(() => ({})); + throw { + status: resp.status, + message: body?.message ?? body?.error + ?? `getGlobalUsage failed with status ${resp.status}`, + ...(body?.code ? { code: body.code } : {}), + }; + } return await resp.json(); } } diff --git a/src/puter-js/tests/api/suites/auth.suite.ts b/src/puter-js/tests/api/suites/auth.suite.ts index 7b0c1ed20..313857d79 100644 --- a/src/puter-js/tests/api/suites/auth.suite.ts +++ b/src/puter-js/tests/api/suites/auth.suite.ts @@ -454,13 +454,42 @@ export default suite('auth', { // -- Usage reporting -- 'deployment-wide usage is refused for a non-admin': async (t) => { - const usage = (await ( - t.puter.auth as unknown as { - getGlobalUsage: () => Promise>; + const error = (await t.assert.rejects(() => + ( + t.puter.auth as unknown as { + getGlobalUsage: () => Promise>; + } + ).getGlobalUsage(), + )) as { status?: number; code?: string; message?: string }; + t.assert.equal(error.status, 403); + t.assert.equal(error.code, 'forbidden'); + t.assert.equal(error.message, 'Only admins may request this resource'); + }, + + 'getDetailedAppUsage rejects for an unknown app': async (t) => { + const error = (await t.assert.rejects(() => + t.puter.auth.getDetailedAppUsage(`no-such-app-${t.puter.randName()}`), + )) as { status?: number; message?: string }; + t.assert.equal(error.status, 404); + t.assert.equal(error.message, 'App not found'); + }, + + // Web handles invalid tokens with a sign-in popup. + 'getMonthlyUsage rejects with a malformed token': { + platforms: ['node', 'workerd'], + fn: async (t) => { + const p = internals(t); + const token = p.authToken; + p.authToken = 'auth-suite-not-a-real-token'; + try { + const error = (await t.assert.rejects(() => + t.puter.auth.getMonthlyUsage(), + )) as { status?: number }; + t.assert.equal(error.status, 401); + } finally { + p.authToken = token; } - ).getGlobalUsage()) as { code?: string; message?: string }; - t.assert.equal(usage.code, 'forbidden'); - t.assert.equal(usage.message, 'Only admins may request this resource'); + }, }, // -- Reauth coordination --