{"record":{"id":"7905bc7264c933c5","repo":"dotnet/reactive","slug":"source-takewhile","errorCode":null,"errorMessage":"source","messagePattern":"source","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeWhile.cs","lineNumber":14,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource> TakeWhile<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (predicate == null)\n                throw new ArgumentNullException(nameof(predicate));\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.TakeWhile(observer, predicate)));\n        }\n\n        public static IAsyncObservable<TSource> TakeWhile<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (predicate == null)\n                throw new ArgumentNullException(nameof(predicate));\n\n            return CreateAsyncObservable<TSource>.From(\n                source,","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/TakeWhile.cs#L1-L32","documentation":"The TakeWhile(source, predicate) operator throws ArgumentNullException because the source observable argument was null. Like all operators in this library, it validates its arguments eagerly when the pipeline is constructed so that a null source surfaces at the TakeWhile call site rather than later during subscription. The message names 'source'.","triggerScenarios":"Calling AsyncObservable.TakeWhile(null, predicate) with a sync Func<TSource, bool> predicate.","commonSituations":"Pipeline built from a repository/service method that returned null instead of an empty stream; nullable collection-to-observable conversions where the input collection was null; conditional query composition where a mandatory step was skipped.","solutions":["Coalesce before filtering: (source ?? AsyncObservable.Empty<TSource>()).TakeWhile(predicate).","Fix the producer of the source to return AsyncObservable.Empty<TSource>() instead of null.","Null-check the source at the pipeline entry and fail with a domain-specific message."],"exampleFix":"// before\nvar taken = source.TakeWhile(x => x > 0);\n// after\nvar taken = (source ?? AsyncObservable.Empty<int>()).TakeWhile(x => x > 0);","handlingStrategy":"validation","validationCode":"if (source == null) source = AsyncObservable.Empty<TSource>();","typeGuard":"bool HasSource<T>(IAsyncObservable<T>? s) => s is not null;","tryCatchPattern":"try { var taken = source.TakeWhile(predicate); } catch (ArgumentNullException ex) when (ex.ParamName == \"source\") { var taken = AsyncObservable.Empty<TSource>(); }","preventionTips":["Return AsyncObservable.Empty<T>() instead of null from stream-producing methods.","Enable nullable reference types and treat null-observable warnings as errors.","Coalesce sources at the top of every pipeline."],"tags":["argument-null","reactive-extensions","takewhile","argument-validation"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}