fromError<T> static method

Either<BaseException, T> fromError<T>(
  1. dynamic error, {
  2. StackTrace? stackTrace,
})

Implementation

static Either<BaseException, T> fromError<T>(dynamic error, {StackTrace? stackTrace}) {
  if (error is! DioException) {
    SeedKitLoggerService.to.e(
      error.runtimeType,
      error: error,
      stackTrace: stackTrace,
    );
    return Left(UnknownException());
  }

  if (error.response?.data != null) {
    return Left(
      DetailException.fromMap(
        error.response?.data,
      ),
    );
  }

  SeedKitLoggerService.to.e(
    error.runtimeType,
    error: error,
    stackTrace: stackTrace,
  );

  switch (error.type) {
    case DioExceptionType.connectionError:
      if (!ConnectivityFactory.to.haveConnection) {
        return Left(NetworkException());
      }

      return Left(NetworkServerException());

    case DioExceptionType.connectionTimeout:
    case DioExceptionType.sendTimeout:
    case DioExceptionType.receiveTimeout:
      return Left(TimeoutException());

    default:
      return Left(UnknownException());
  }
}