refactor: pull validateConnection / validateModelName / error mapping into base #8
Reference in New Issue
Block a user
Delete Branch "refactor/template-method-pullup"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Form Template Method / Pull Up Method from the Refactoring Guru catalog.
The three SDK-based providers (Claude, OpenAI, Gemini) each had their own
near-identical implementations of:
of patterns, warn on the rest
brand-flavored messages
These collapse into three protected hooks on BaseAIProvider:
The base owns the wrapping logic (warning, status -> error-type map,
common message patterns). Providers only contribute their unique parts.
Side effects:
so provider-specific patterns work via the existing try/catch in
BaseAIProvider.complete/stream. The per-provider try/catch wrappers
around doComplete/doStream are removed.
${providerName} error: ${msg}instead of the hardcoded
Provider error:/Claude API error:etc.since its non-SDK shape doesn't fit the SDK probe pattern.
Form Template Method / Pull Up Method from the Refactoring Guru catalog. The three SDK-based providers (Claude, OpenAI, Gemini) each had their own near-identical implementations of: - validateConnection: try a tiny request, catch errors, map a handful of patterns, warn on the rest - validateModelName: iterate regex patterns, warn on no match - handle{X}Error: switch on HTTP status, map to AIProviderError with brand-flavored messages These collapse into three protected hooks on BaseAIProvider: - getModelNamePatterns(): RegExp[] - sendValidationProbe(): Promise<void> - mapProviderError(error): AIProviderError | null - providerErrorMessages(): Partial<Record<number, string>> The base owns the wrapping logic (warning, status -> error-type map, common message patterns). Providers only contribute their unique parts. Side effects: - normalizeError now consults mapProviderError before generic mapping, so provider-specific patterns work via the existing try/catch in BaseAIProvider.complete/stream. The per-provider try/catch wrappers around doComplete/doStream are removed. - The unknown-error message becomes `${providerName} error: ${msg}` instead of the hardcoded `Provider error:` / `Claude API error:` etc. - OpenWebUI's HTTP-based validateConnection is preserved (override), since its non-SDK shape doesn't fit the SDK probe pattern.