From ad1ffeb2e865a12f6bb9a759df36b9dd7a9947d4 Mon Sep 17 00:00:00 2001 From: sofyan48 Date: Sun, 29 Nov 2026 23:08:30 +0000 Subject: [PATCH] Localize upstream billing catalog preview failures in Vue UI --- composables/billing/importError.test.ts | 17 +++++++++++++++++ composables/billing/importError.ts | 7 +++++++ composables/billing/useBillingPriceImport.ts | 3 ++- locales/admin/billing.ts | 2 ++ locales/admin/ru/billing.ts | 1 + locales/admin/zh-CN/billing.ts | 1 + locales/admin/zh-TW/billing.ts | 1 + 7 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 composables/billing/importError.test.ts create mode 100644 composables/billing/importError.ts diff --git a/composables/billing/importError.test.ts b/composables/billing/importError.test.ts new file mode 100644 index 0000000..8ece891 --- /dev/null +++ b/composables/billing/importError.test.ts @@ -0,0 +1,17 @@ +import { expect, test } from 'bun:test' +import { readFileSync } from 'node:fs' +import { isImportCatalogUnavailable } from './importError' + +test('recognizes Home catalog fetch failures without exposing diagnostic messages', () => { + expect(isImportCatalogUnavailable({ statusCode: 502, data: { error: 'models_dev_fetch_failed', message: 'private catalog URL' } })).toBe(true) +}) +test('does not misclassify other gateway errors or unsupported deployments', () => { + for (const cause of [null, 'models_dev_fetch_failed', {}, { statusCode: 502 }, { statusCode: 502, data: 'models_dev_fetch_failed' }, { statusCode: 500, data: { error: 'models_dev_fetch_failed' } }, { statusCode: 502, data: { error: 'billing_import_preview_failed' } }, { statusCode: 404 }]) { + expect(isImportCatalogUnavailable(cause)).toBe(false) + } +}) +test('preview localizes catalog failures while retaining unsupported endpoint fallback', () => { + const source = readFileSync(new URL('./useBillingPriceImport.ts', import.meta.url), 'utf8') + expect(source).toContain("isImportCatalogUnavailable(cause) ? t('billing.import.err.catalog')") + expect(source).toContain('[404, 405, 501].includes(cause?.statusCode)') +}) diff --git a/composables/billing/importError.ts b/composables/billing/importError.ts new file mode 100644 index 0000000..17a0520 --- /dev/null +++ b/composables/billing/importError.ts @@ -0,0 +1,7 @@ +// Home returns a stable error code separately from its diagnostic message. +export function isImportCatalogUnavailable(cause: unknown): boolean { + if (!cause || typeof cause !== 'object') return false + const error = cause as { statusCode?: unknown; data?: unknown } + if (error.statusCode !== 502 || !error.data || typeof error.data !== 'object') return false + return (error.data as { error?: unknown }).error === 'models_dev_fetch_failed' +} diff --git a/composables/billing/useBillingPriceImport.ts b/composables/billing/useBillingPriceImport.ts index 54b1a51..91980d5 100644 --- a/composables/billing/useBillingPriceImport.ts +++ b/composables/billing/useBillingPriceImport.ts @@ -1,3 +1,4 @@ +import { isImportCatalogUnavailable } from './importError' import { resolveImportSelection } from './importSelection' import { scopeImportMatches } from './importMatches' import { billingImportApplySupportError, createBillingImportIdempotencyKey, billingImportStorageKey, isDefinitiveBillingImportFailure, readBillingImportPending, validateBillingImportResult, writeBillingImportPending, type BillingImportPendingApply } from '~/composables/billingImport' @@ -318,7 +319,7 @@ async function previewPrices(updates = false, preferredSelection?: string[]) { } catch (cause: any) { if (!disposed && session === sessionVersion && !controller.signal.aborted && version === inputVersion) { if ([404, 405, 501].includes(cause?.statusCode)) importUnavailable.value = true - else error.value = cause?.message || t('billing.import.err.preview') + else error.value = isImportCatalogUnavailable(cause) ? t('billing.import.err.catalog') : cause?.message || t('billing.import.err.preview') } } finally { if (!disposed && session === sessionVersion && previewController === controller) { diff --git a/locales/admin/billing.ts b/locales/admin/billing.ts index 07dc4ed..9fd51ce 100644 --- a/locales/admin/billing.ts +++ b/locales/admin/billing.ts @@ -400,6 +400,7 @@ export default defineAdminMessages({ 'billing.import.err.rowMultiplier': 'Row multipliers need distinct nonempty keys and positive values.', 'billing.import.err.override': 'Each match override must identify a distinct requested target and a catalog provider/model.', 'billing.import.err.preview': 'Unable to preview model prices.', + 'billing.import.err.catalog': 'Unable to load the price catalog. Please try again later.', 'billing.import.err.initializing': 'Import recovery is still initializing. Wait before applying prices.', 'billing.import.err.saveRecovery': 'Unable to save the recovery request. Nothing was sent; enable session storage before applying.', 'billing.import.err.clearRecovery': 'Unable to clear the saved recovery request. It has been retained.', @@ -817,6 +818,7 @@ export default defineAdminMessages({ 'billing.import.err.rowMultiplier': 'Pengali baris memerlukan key berbeda yang tidak kosong dan nilai positif.', 'billing.import.err.override': 'Setiap override pencocokan harus menunjuk target berbeda yang diminta dan provider/model katalog.', 'billing.import.err.preview': 'Gagal membuat pratinjau harga model.', + 'billing.import.err.catalog': 'Gagal memuat katalog harga. Coba lagi nanti.', 'billing.import.err.initializing': 'Pemulihan impor masih diinisialisasi. Tunggu sebelum menerapkan harga.', 'billing.import.err.saveRecovery': 'Gagal menyimpan request pemulihan. Tidak ada yang dikirim; aktifkan session storage sebelum menerapkan.', 'billing.import.err.clearRecovery': 'Gagal menghapus request pemulihan tersimpan. Request tersebut tetap disimpan.', diff --git a/locales/admin/ru/billing.ts b/locales/admin/ru/billing.ts index 049b2e4..b08f538 100644 --- a/locales/admin/ru/billing.ts +++ b/locales/admin/ru/billing.ts @@ -401,6 +401,7 @@ export default { 'billing.import.err.rowMultiplier': 'Для множителей строк нужны уникальные непустые ключи и положительные значения.', 'billing.import.err.override': 'Каждое переопределение сопоставления должно указывать уникальную запрошенную цель и провайдера/модель каталога.', 'billing.import.err.preview': 'Не удалось выполнить предпросмотр цен моделей.', + 'billing.import.err.catalog': 'Не удалось загрузить каталог цен. Повторите попытку позже.', 'billing.import.err.initializing': 'Восстановление импорта ещё инициализируется. Подождите перед применением цен.', 'billing.import.err.saveRecovery': 'Не удалось сохранить запрос восстановления. Ничего не отправлено; включите session storage перед применением.', 'billing.import.err.clearRecovery': 'Не удалось очистить сохранённый запрос восстановления. Он сохранён.', diff --git a/locales/admin/zh-CN/billing.ts b/locales/admin/zh-CN/billing.ts index 01bde47..1aa4f3a 100644 --- a/locales/admin/zh-CN/billing.ts +++ b/locales/admin/zh-CN/billing.ts @@ -401,6 +401,7 @@ export default { 'billing.import.err.rowMultiplier': '行倍率需要互不相同的非空键和正数值。', 'billing.import.err.override': '每个匹配覆盖必须指定唯一的请求目标和目录提供商/模型。', 'billing.import.err.preview': '无法预览模型价格。', + 'billing.import.err.catalog': '无法加载价格目录,请稍后重试。', 'billing.import.err.initializing': '导入恢复仍在初始化。请稍候再应用价格。', 'billing.import.err.saveRecovery': '无法保存恢复请求。未发送任何内容;请先启用会话存储再应用。', 'billing.import.err.clearRecovery': '无法清除已保存的恢复请求,已予以保留。', diff --git a/locales/admin/zh-TW/billing.ts b/locales/admin/zh-TW/billing.ts index 02b9c51..255bb90 100644 --- a/locales/admin/zh-TW/billing.ts +++ b/locales/admin/zh-TW/billing.ts @@ -401,6 +401,7 @@ export default { 'billing.import.err.rowMultiplier': '列倍率需要不重複的非空鍵值與正數值。', 'billing.import.err.override': '每個比對覆寫都必須指定不重複的請求目標,以及目錄供應商/模型。', 'billing.import.err.preview': '無法預覽模型價格。', + 'billing.import.err.catalog': '無法載入價格目錄,請稍後重試。', 'billing.import.err.initializing': '匯入復原仍在初始化中。請稍候再套用價格。', 'billing.import.err.saveRecovery': '無法儲存復原請求。未傳送任何內容;套用前請啟用工作階段儲存空間。', 'billing.import.err.clearRecovery': '無法清除已儲存的復原請求,已保留該請求。',