createProviderSafely<P> method

T createProviderSafely<P>(
  1. LLMConfig config,
  2. P configFactory(),
  3. T providerFactory(
    1. P
    )
)

Helper method for creating provider instances with error handling

Implementation

T createProviderSafely<P>(
  LLMConfig config,
  P Function() configFactory,
  T Function(P) providerFactory,
) {
  try {
    validateConfigWithDetails(config);
    final providerConfig = configFactory();
    return providerFactory(providerConfig);
  } catch (e) {
    if (e is LLMError) {
      rethrow;
    }
    throw GenericError(
        'Failed to create $displayName provider: ${e.toString()}');
  }
}