guardWebExceptions<R> function

R guardWebExceptions<R>(
  1. R cb(), {
  2. required String plugin,
  3. required String codeParser(
    1. String
    ),
  4. String messageParser(
    1. String code,
    2. String message
    )?,
})

Will return a FirebaseException from a thrown web error. Any other errors will be propagated as normal.

Implementation

R guardWebExceptions<R>(
  R Function() cb, {
  required String plugin,
  required String Function(String) codeParser,
  String Function(String code, String message)? messageParser,
}) {
  try {
    final value = cb();

    if (value is Future) {
      return value.catchError(
        (err, stack) => Error.throwWithStackTrace(
          _mapException(
            err,
            plugin: plugin,
            codeParser: codeParser,
            messageParser: messageParser,
          ),
          stack,
        ),
        test: _testException,
      ) as R;
    } else if (value is Stream) {
      return value.handleError(
        (err, stack) => Error.throwWithStackTrace(
          _mapException(
            err,
            plugin: plugin,
            codeParser: codeParser,
            messageParser: messageParser,
          ),
          stack,
        ),
        test: _testException,
      ) as R;
    }

    return value;
  } catch (error, stack) {
    if (!_testException(error)) {
      // Make sure to preserve the stacktrace
      rethrow;
    }

    Error.throwWithStackTrace(
      _mapException(
        error,
        plugin: plugin,
        codeParser: codeParser,
        messageParser: messageParser,
      ),
      stack,
    );
  }
}