{"record":{"id":"1682f224faf966ca","repo":"dotnet/reactive","slug":"nameof-observer","errorCode":null,"errorMessage":"nameof(observer)","messagePattern":"nameof\\(observer\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Do.cs","lineNumber":238,"sourceCode":"                throw new ArgumentNullException(nameof(onError));\n\n            return Do(observer, Create<TSource>(_ => default, onError, () => default));\n        }\n\n        public static IAsyncObserver<TSource> Do<TSource>(IAsyncObserver<TSource> observer, Func<ValueTask> onCompleted)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (onCompleted == null)\n                throw new ArgumentNullException(nameof(onCompleted));\n\n            return Do(observer, Create<TSource>(_ => default, _ => default, onCompleted));\n        }\n\n        public static IAsyncObserver<TSource> Do<TSource>(IAsyncObserver<TSource> observer, Func<TSource, ValueTask> onNext, Func<Exception, ValueTask> onError, Func<ValueTask> onCompleted)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (onNext == null)\n                throw new ArgumentNullException(nameof(onNext));\n            if (onError == null)\n                throw new ArgumentNullException(nameof(onError));\n            if (onCompleted == null)\n                throw new ArgumentNullException(nameof(onCompleted));\n\n            return Do(observer, Create(onNext, onError, onCompleted));\n        }\n\n        public static IAsyncObserver<TSource> Do<TSource>(IAsyncObserver<TSource> observer, IObserver<TSource> witness)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (witness == null)\n                throw new ArgumentNullException(nameof(witness));\n\n            return Create<TSource>(","sourceCodeStart":220,"sourceCodeEnd":256,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Do.cs#L220-L256","documentation":"The full Do(observer, onNext, onError, onCompleted) overload throws ArgumentNullException when the observer argument is null. This overload installs separate handlers for each notification kind while forwarding to the observer, so the observer is required and is validated first, before the callback arguments.","triggerScenarios":"Calling AsyncObserver.Do<TSource>(null, onNext, onError, onCompleted) — null first argument even with all three callbacks supplied.","commonSituations":"Generic pipeline-builder helpers that pass their observer parameter straight through when the caller supplied null; composition happening before the downstream consumer is created in async initialization flows; DI/container resolution silently returning null for the observer dependency.","solutions":["Provide a non-null observer; if only the three callbacks matter, use AsyncObserver.Create(onNext, onError, onCompleted) as the observer (possibly chained with Do).","Fix the null source: verify the caller of your builder passes a real observer and that DI registration for it exists.","Add a guard or contract check at your own API boundary so null observers fail with a clearer domain message."],"exampleFix":"// before\nvar obs = AsyncObserver.Do<int>(resolveObserver(), onX, onE, onC); // resolveObserver() returned null\n\n// after\nvar downstream = resolveObserver() ?? AsyncObserver.Create<int>(_ => default, _ => default, () => default);\nvar obs = AsyncObserver.Do<int>(downstream, onX, onE, onC);","handlingStrategy":"validation","validationCode":"if (observer is null)\n    throw new InvalidOperationException(\"pipeline requires a non-null downstream observer\");\nvar result = AsyncObserver.Do(source, observer, onNext, onError, onCompleted);","typeGuard":"static bool AllArgsValid<T>(IAsyncObserver<T>? o, Func<T, ValueTask>? n, Func<Exception, ValueTask>? e, Func<ValueTask>? c) => o is not null && n is not null && e is not null && c is not null;","tryCatchPattern":"try\n{\n    var obs = AsyncObserver.Do(source, observer, onNext, onError, onCompleted);\n}\ncatch (ArgumentNullException ex)\n{\n    log.LogError(ex, \"Do composition failed on parameter {Param}\", ex.ParamName);\n}","preventionTips":["Validate caller-supplied observers at your own API boundary before forwarding to Do","Check DI/container configuration so observer dependencies never resolve to null","Compose the pipeline only after the consumer is fully constructed","Write a composition unit test that fails fast on null arguments"],"tags":["null-argument","argument-validation","async-reactive"],"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"}