diff --git a/src/entities/Font/model/store/unifiedFontStore/unifiedFontStore.svelte.ts b/src/entities/Font/model/store/unifiedFontStore/unifiedFontStore.svelte.ts index 8ba082f..b7c9e99 100644 --- a/src/entities/Font/model/store/unifiedFontStore/unifiedFontStore.svelte.ts +++ b/src/entities/Font/model/store/unifiedFontStore/unifiedFontStore.svelte.ts @@ -15,6 +15,10 @@ import type { QueryObserverOptions } from '@tanstack/query-core'; import type { ProxyFontsParams } from '../../../api'; import { fetchProxyFonts } from '../../../api'; +import { + FontNetworkError, + FontResponseError, +} from '../../../lib/errors/errors'; import type { UnifiedFont } from '../../types'; import { BaseFontStore } from '../baseFontStore/baseFontStore.svelte'; @@ -188,36 +192,29 @@ export class UnifiedFontStore extends BaseFontStore { * Returns the full response including pagination metadata */ protected async fetchFn(params: ProxyFontsParams): Promise { - const response = await fetchProxyFonts(params); + let response: Awaited>; + try { + response = await fetchProxyFonts(params); + } catch (cause) { + throw new FontNetworkError(cause); + } - // Validate response structure if (!response) { - console.error('[UnifiedFontStore] fetchProxyFonts returned undefined', { params }); - throw new Error('Proxy API returned undefined response'); + throw new FontResponseError('response', response); } - if (!response.fonts) { - console.error('[UnifiedFontStore] response.fonts is undefined', { response }); - throw new Error('Proxy API response missing fonts array'); + throw new FontResponseError('response.fonts', response.fonts); } - if (!Array.isArray(response.fonts)) { - console.error('[UnifiedFontStore] response.fonts is not an array', { - fonts: response.fonts, - }); - throw new Error('Proxy API fonts is not an array'); + throw new FontResponseError('response.fonts', response.fonts); } - // Store pagination metadata separately for derived values this.#paginationMetadata = { total: response.total ?? 0, limit: response.limit ?? this.params.limit ?? 50, offset: response.offset ?? this.params.offset ?? 0, }; - // Accumulate fonts for infinite scroll - // Note: For offset === 0, we rely on the $effect above to handle the reset/init - // This prevents race conditions and double-setting. if (params.offset !== 0) { this.#accumulatedFonts = [...this.#accumulatedFonts, ...response.fonts]; }