FirebaseAuthAdminException.fromServer constructor

FirebaseAuthAdminException.fromServer(
  1. String serverMessage, {
  2. int? httpStatus,
})

Maps the error message of the Identity Toolkit API (serverMessage, for example USER_NOT_FOUND or INVALID_EMAIL : bad address) to an exception, like the Node Admin SDK does.

final FirebaseAuthAdminException e = FirebaseAuthAdminException.fromServer(
  'DUPLICATE_LOCAL_ID',
  httpStatus: 400,
);
print(e.code); // uid-already-exists

Implementation

factory FirebaseAuthAdminException.fromServer(
  String serverMessage, {
  int? httpStatus,
}) {
  final int separator = serverMessage.indexOf(' : ');
  final String serverCode =
      (separator < 0 ? serverMessage : serverMessage.substring(0, separator))
          .trim();
  final String? detail = separator < 0
      ? null
      : serverMessage.substring(separator + 3).trim();
  final String? code = _codes[serverCode];
  return FirebaseAuthAdminException(
    code: code ?? 'internal-error',
    message:
        detail ??
        (code == null
            ? 'The backend answered with the error $serverCode.'
            : _messages[code] ?? 'The backend answered with $serverCode.'),
    serverCode: serverCode,
    httpStatus: httpStatus,
  );
}