{"record":{"id":"f74310754a6f275a","repo":"dotnet/reactive","slug":"nameof-onerror","errorCode":null,"errorMessage":"nameof(onError)","messagePattern":"nameof\\(onError\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Do.cs","lineNumber":242,"sourceCode":"\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>(\n                async x =>\n                {\n                    try\n                    {","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Do.cs#L224-L260","documentation":"System.Reactive.Async's Do operator requires a non-null onError callback so that error notifications from the source sequence can be forwarded to the witness observer. Passing null makes it impossible to build the observer delegate, so the library throws ArgumentNullException immediately, before any subscription happens. This is a fail-fast programming-contract check, not a runtime data error.","triggerScenarios":"Calling the Do<TSource>(IAsyncObserver<TSource> observer, Func<TSource,Task> onNext, Func<Exception,Task> onError, Func<Task> onCompleted) overload (Do.cs:242) with a null onError argument while the other three arguments are non-null.","commonSituations":"Developers build handler lambdas conditionally (e.g. only wiring onNext and onCompleted) and pass a null onError; refactoring from the simpler Do(observer, onNext) overload to the full triple-callback overload and forgetting to supply the error handler; interop code that maps an external observer whose error handler is null.","solutions":["Pass a non-null onError handler; use a no-op such as ex => { } (or ex => Task.CompletedTask for the async overload) if you do not care about errors.","Log the error in onError rather than dropping it, so silent failure is avoided.","Add a null check or default argument at the call site before invoking Do."],"exampleFix":"// before\nvar observer = Do(src, OnNextAsync, null, OnCompletedAsync);\n// after\nvar observer = Do(src, OnNextAsync, ex => Console.WriteLine(ex), OnCompletedAsync);","handlingStrategy":"validation","validationCode":"if (onError == null) onError = ex => { }; // or assert: ArgumentNullException.ThrowIfNull(onError) at your own boundary\nvar observed = Do(observer, onNext, onError, onCompleted);","typeGuard":"bool IsValidHandler(Delegate d) => d is not null;","tryCatchPattern":"try\n{\n    var observed = Do(observer, onNext, onError, onCompleted);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"onError\")\n{\n    logger.LogWarning(\"Do() called without an error handler; using no-op\");\n    var observed = Do(observer, onNext, ex2 => { }, onCompleted);\n}","preventionTips":["Never pass null for observer callbacks; prefer explicit no-op lambdas so intent is visible.","Wrap operator composition in a helper that fills in default handlers.","Enable nullable reference types so null delegate flow is caught at compile time."],"tags":["argument-null","rx","do-operator","csharp"],"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"}