fromPlatformCode static method

ErrorCode fromPlatformCode(
  1. dynamic platformCode,
  2. IapPlatform platform
)

Maps a platform-specific error code back to the standardized ErrorCode enum

Implementation

static ErrorCode fromPlatformCode(
  dynamic platformCode,
  IapPlatform platform,
) {
  // Handle string codes (Android E_* codes or OpenIAP kebab-case)
  if (platformCode is String) {
    // First try direct mapping from ErrorCodeMapping
    const mapping = ErrorCodeMapping.android;
    for (final entry in mapping.entries) {
      if (entry.value == platformCode) {
        return entry.key;
      }
    }

    // If starts with E_, try to normalize and find match
    if (platformCode.startsWith('E_')) {
      final withoutE = platformCode.substring(2);
      final kebabCased = _toKebabCase(withoutE);
      final withE = 'E_${kebabCased.toUpperCase().replaceAll('-', '_')}';

      // Try to find in mapping again with normalized form
      for (final entry in mapping.entries) {
        if (entry.value == withE) {
          return entry.key;
        }
      }
    }

    // Try to parse as OpenIAP kebab-case
    try {
      return ErrorCode.fromJson(platformCode);
    } catch (_) {
      // Fall back to unknown
    }

    return ErrorCode.Unknown;
  }

  // Handle legacy/numeric iOS codes
  if (platformCode is int) {
    const mapping = ErrorCodeMapping.ios;
    for (final entry in mapping.entries) {
      if (entry.value == platformCode) {
        return entry.key;
      }
    }
  }

  return ErrorCode.Unknown;
}