dvFcmErrorCode function

String? dvFcmErrorCode(
  1. String body
)

Pulls FCM's FcmError.errorCode (or, failing that, the canonical status) out of an error body. Null when the body says neither.

Implementation

String? dvFcmErrorCode(String body) {
  try {
    final Object? decoded = jsonDecode(body);
    if (decoded is! Map<String, Object?>) return null;
    final Object? error = decoded['error'];
    if (error is! Map<String, Object?>) return null;
    final Object? details = error['details'];
    if (details is List) {
      for (final Object? detail in details) {
        if (detail is Map<String, Object?> && detail['errorCode'] is String) {
          return detail['errorCode']! as String;
        }
      }
    }
    final Object? status = error['status'];
    return status is String ? status : null;
  } on FormatException {
    return null;
  }
}