FirebaseAuthAdminException.fromServerError constructor

FirebaseAuthAdminException.fromServerError({
  1. required String serverErrorCode,
  2. Object? rawServerResponse,
})

Implementation

factory FirebaseAuthAdminException.fromServerError({
  required String serverErrorCode,
  Object? rawServerResponse,
}) {
  // serverErrorCode could contain additional details:
  // ERROR_CODE : Detailed message which can also contain colons
  final colonSeparator = serverErrorCode.indexOf(':');
  String? customMessage;
  var effectiveErrorCode = serverErrorCode;
  if (colonSeparator != -1) {
    customMessage = serverErrorCode.substring(colonSeparator + 1).trim();
    // Treat empty string as null
    if (customMessage.isEmpty) customMessage = null;
    effectiveErrorCode = serverErrorCode.substring(0, colonSeparator).trim();
  }
  // If not found, default to internal error.
  final error =
      authServerToClientCode[effectiveErrorCode] ??
      AuthClientErrorCode.internalError;
  // Server detailed message should have highest priority.
  customMessage = customMessage ?? error.message;

  if (error == AuthClientErrorCode.internalError &&
      rawServerResponse != null) {
    try {
      customMessage +=
          ' Raw server response: "${jsonEncode(rawServerResponse)}"';
    } catch (e) {
      // Ignore JSON parsing error.
    }
  }

  return FirebaseAuthAdminException(error, customMessage);
}