feat: Implement typed error contracts in generic hooks
Introduce discriminated FetchError union type to replace weak string error handling in API calls and hooks. Enables actionable error diagnostics. Changes: - Create types/api.ts with FetchError discriminated union (api_error, network_error, abort_error) - Export type guards: isAuthError, isAbortError, isNetworkError, isApiError - Update useListData and usePolledData to expose typed FetchError instead of string - Add getErrorMessage() helper to extract displayable messages from FetchError - Add createStringErrorAdapter() for backward compatibility with string error state - Update handleFetchError() to work with both FetchError and string setters - Update all consumer hooks to expose typed errors - Update components to use getErrorMessage() when displaying errors - Update tests to mock FetchError instead of strings - Add comprehensive typed error model documentation to Web-Development.md This enables better error handling patterns: - Check error.type to distinguish between API, network, and abort errors - Extract status codes for specific handling (401/403 auth, 50x server errors) - Maintain backward compatibility with existing string-based error states All TypeScript compilation passes with no errors. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { fetchGlobalConfig, updateGlobalConfig } from "../api/config";
|
||||
import { handleFetchError } from "../utils/fetchError";
|
||||
import { handleFetchError, createStringErrorAdapter } from "../utils/fetchError";
|
||||
import type { GlobalConfig, GlobalConfigUpdate } from "../types/config";
|
||||
|
||||
export interface UseGlobalConfigResult {
|
||||
@@ -39,7 +39,7 @@ export function useGlobalConfig(): UseGlobalConfigResult {
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
if (!ctrl.signal.aborted) {
|
||||
handleFetchError(err, setError, "Failed to fetch global config");
|
||||
handleFetchError(err, createStringErrorAdapter(setError), "Failed to fetch global config");
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
|
||||
Reference in New Issue
Block a user