refactor: pull validateConnection / validateModelName / error mapping into base #8

Merged
jleibl merged 1 commits from refactor/template-method-pullup into main 2026-05-21 11:46:55 +00:00
Owner

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
  • 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.
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.
jleibl added 1 commit 2026-05-21 11:46:49 +00:00
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.
jleibl merged commit eacd76d259 into main 2026-05-21 11:46:55 +00:00
jleibl deleted branch refactor/template-method-pullup 2026-05-21 11:46:59 +00:00
jleibl referenced this issue from a commit 2026-05-21 11:54:34 +00:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: jleibl/simple-ai-provider#8