parseError function

String? parseError(
  1. Map<String, dynamic>? body
)

Parses an error message from a JSON response body.

This function takes a JSON response body as input and attempts to parse an error message from it. It looks for the presence of 'errors' or 'error' keys in the input body and extracts the corresponding error value. If an error message is found, it is returned as a String. If no error message is found, or if the parsing process encounters unexpected data types, null is returned.

@param body The JSON response body from which to parse an error message. @return A parsed error message as a String, or null if no error message is found.

Implementation

String? parseError(Map<String, dynamic>? body) {
  if (body == null) return null;

  final Object? error =
      body.containsKey('errors') ? body['errors'] : body['error'];

  if (error == null) return null;

  if (error is List<dynamic>) {
    final Map<String, dynamic> firstEntry = error.first as Map<String, dynamic>;
    return firstEntry['message'] as String;
  }

  if (error is String) {
    return error.contains(RegExp('ID token', caseSensitive: false))
        ? kLoginLogoutPrompt
        : error;
  }

  return null;
}