mediaFailureCause function

String? mediaFailureCause(
  1. String url,
  2. Object error
)

The health cause for a media error, or null for network and 5xx noise.

Implementation

String? mediaFailureCause(String url, Object error) {
  if (isInvalidMediaUrl(url)) return 'invalid_url';
  if (error is FormatException) return 'decode';
  // `decodeMediaLottie` rethrows every parse failure of the downloaded bytes
  // as a FormatException, so a corrupt composition lands in the arm above
  // without matching library type names, which release builds can obfuscate.
  // These media libraries expose no typed HTTP status here.
  final message = '$error';
  if (RegExp(r'(statusCode: |status |Response code: )4\d\d')
      .hasMatch(message)) {
    return 'http_4xx';
  }
  // The engine words it "Codec failed to produce an image, possibly due to
  // invalid image data": match without regard to case.
  if (message.toLowerCase().contains('invalid image data')) return 'decode';
  return null;
}