tryMap method

  1. @override
Failure? tryMap(
  1. Object error, [
  2. StackTrace? stackTrace
])
override

Пытается преобразовать error в Failure на основе его типа и сообщения.

Если error уже является Failure, возвращает его без изменений. Для dart:io ошибок использует PlatformExceptions для кроссплатформенной проверки типов.

Возвращает: Failure соответствующего типа или null, если ошибка не распознана.

Implementation

@override
Failure? tryMap(Object error, [StackTrace? stackTrace]) {
  if (error is Failure) return error;

  // `dart:io` types are probed, not pattern-matched: they do not exist on
  // the web and the probes keep evaluation order identical to the previous
  // switch.
  if (PlatformExceptions.isSocketException(error)) {
    return _mapSocketMessage(PlatformExceptions.socketMessage(error));
  }
  if (error is TimeoutException) {
    return const Failure.network(type: NetworkFailure.timeout);
  }
  if (PlatformExceptions.isHandshakeException(error) ||
      PlatformExceptions.isTlsException(error)) {
    return const Failure.network(type: NetworkFailure.badCertificate);
  }
  if (error is FormatException) return _mapFormatException(error);
  if (PlatformExceptions.isHttpException(error)) {
    return _mapHttpMessage(PlatformExceptions.httpMessage(error) ?? '');
  }

  return switch (error) {
    UnsupportedError e => Failure.platform(
      type: PlatformFailure.notSupported,
      details: e.message,
    ),
    ArgumentError e => Failure.validation(
      type: ValidationFailure.invalidFormat,
      message: e.message?.toString() ?? e.toString(),
    ),
    StateError e => Failure.platform(
      type: PlatformFailure.osError,
      details: e.message,
    ),
    TypeError _ => Failure.parse(
      type: ParseFailure.unexpectedType,
      message: error.toString(),
    ),
    Exception e => Failure.unknown(
      error: e,
      stackTrace: stackTrace,
      message: e.toString(),
    ),
    Error e => Failure.unknown(
      error: e,
      stackTrace: stackTrace,
      message: e.toString(),
    ),
    _ => null,
  };
}