feat(UnifiedFontStore): throw FontNetworkError and FontResponseError in fetchFn

This commit is contained in:
Ilia Mashkov
2026-04-05 14:06:32 +03:00
parent a1a1fcf39d
commit df3c694909

View File

@@ -15,6 +15,10 @@
import type { QueryObserverOptions } from '@tanstack/query-core'; import type { QueryObserverOptions } from '@tanstack/query-core';
import type { ProxyFontsParams } from '../../../api'; import type { ProxyFontsParams } from '../../../api';
import { fetchProxyFonts } from '../../../api'; import { fetchProxyFonts } from '../../../api';
import {
FontNetworkError,
FontResponseError,
} from '../../../lib/errors/errors';
import type { UnifiedFont } from '../../types'; import type { UnifiedFont } from '../../types';
import { BaseFontStore } from '../baseFontStore/baseFontStore.svelte'; import { BaseFontStore } from '../baseFontStore/baseFontStore.svelte';
@@ -188,36 +192,29 @@ export class UnifiedFontStore extends BaseFontStore<ProxyFontsParams> {
* Returns the full response including pagination metadata * Returns the full response including pagination metadata
*/ */
protected async fetchFn(params: ProxyFontsParams): Promise<UnifiedFont[]> { protected async fetchFn(params: ProxyFontsParams): Promise<UnifiedFont[]> {
const response = await fetchProxyFonts(params); let response: Awaited<ReturnType<typeof fetchProxyFonts>>;
try {
response = await fetchProxyFonts(params);
} catch (cause) {
throw new FontNetworkError(cause);
}
// Validate response structure
if (!response) { if (!response) {
console.error('[UnifiedFontStore] fetchProxyFonts returned undefined', { params }); throw new FontResponseError('response', response);
throw new Error('Proxy API returned undefined response');
} }
if (!response.fonts) { if (!response.fonts) {
console.error('[UnifiedFontStore] response.fonts is undefined', { response }); throw new FontResponseError('response.fonts', response.fonts);
throw new Error('Proxy API response missing fonts array');
} }
if (!Array.isArray(response.fonts)) { if (!Array.isArray(response.fonts)) {
console.error('[UnifiedFontStore] response.fonts is not an array', { throw new FontResponseError('response.fonts', response.fonts);
fonts: response.fonts,
});
throw new Error('Proxy API fonts is not an array');
} }
// Store pagination metadata separately for derived values
this.#paginationMetadata = { this.#paginationMetadata = {
total: response.total ?? 0, total: response.total ?? 0,
limit: response.limit ?? this.params.limit ?? 50, limit: response.limit ?? this.params.limit ?? 50,
offset: response.offset ?? this.params.offset ?? 0, 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) { if (params.offset !== 0) {
this.#accumulatedFonts = [...this.#accumulatedFonts, ...response.fonts]; this.#accumulatedFonts = [...this.#accumulatedFonts, ...response.fonts];
} }