mapError static method

DSIntelliToggleException mapError(
  1. dynamic error
)

Maps an error to a DSIntelliToggleException.

Implementation

static DSIntelliToggleException mapError(dynamic error) {
  if (error is DSIntelliToggleException) return error;

  final message = error.toString().toLowerCase();

  if (message.contains('unauthorized') || message.contains('401')) {
    return DSIntelliToggleException(
      code: 'unauthorized',
      message: 'Invalid OAuth2 credentials. Check your clientId, clientSecret, and tenantId.',
    );
  }

  if (message.contains('not found') || message.contains('404')) {
    return DSIntelliToggleException(
      code: 'flag_not_found',
      message: 'Feature flag not found in IntelliToggle.',
    );
  }

  if (message.contains('rate limit') || message.contains('429')) {
    return DSIntelliToggleException(
      code: 'rate_limit_exceeded',
      message: 'IntelliToggle API rate limit exceeded. Try again later.',
    );
  }

  if (message.contains('timeout')) {
    return DSIntelliToggleException(
      code: 'timeout',
      message: 'IntelliToggle API request timed out.',
    );
  }

  if (message.contains('network') || message.contains('socket')) {
    return DSIntelliToggleException(
      code: 'network_error',
      message: 'Network error while connecting to IntelliToggle.',
    );
  }

  if (message.contains('not initialized')) {
    return DSIntelliToggleException(
      code: 'not_initialized',
      message: 'DSIntelliToggleProvider is not initialized. Call initialize() first.',
    );
  }

  return DSIntelliToggleException(
    code: 'unknown_error',
    message: 'An unexpected error occurred: ${error.toString()}',
  );
}