TrustPinException.fromPlatformException constructor

TrustPinException.fromPlatformException(
  1. dynamic error
)

Creates a TrustPinException from a platform exception.

This factory constructor is used internally to convert platform-specific exceptions into TrustPinException instances.

Implementation

factory TrustPinException.fromPlatformException(dynamic error) {
  if (error.toString().contains('PlatformException')) {
    // Extract error information from PlatformException string representation
    final errorString = error.toString();
    final codeMatch = RegExp(r'code: ([^,]+)').firstMatch(errorString);
    final messageMatch = RegExp(r'message: ([^,]+)').firstMatch(errorString);

    final code = codeMatch?.group(1) ?? 'UNKNOWN_ERROR';
    final message = messageMatch?.group(1) ?? error.toString();

    return TrustPinException(code, message);
  }

  return TrustPinException('UNKNOWN_ERROR', error.toString());
}