{"record":{"id":"7f4031c1441107fb","repo":"dotnet/reactive","slug":"the-asynchronous-operation-failed-with-a-null-error-code","errorCode":null,"errorMessage":"The asynchronous operation failed with a null error code.","messagePattern":"The asynchronous operation failed with a null error code\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Foundation/AsyncInfoToObservableBridge.cs","lineNumber":49,"sourceCode":"                progress?.Report(p);\n            });\n\n            Done(info, info.Status, true);\n        }\n\n        private void Done(IAsyncInfo info, AsyncStatus status, bool initial)\n        {\n            var error = default(Exception);\n            var result = default(TResult);\n\n            //\n            // Initial interactions with the IAsyncInfo object. Those could fail, which indicates\n            // a rogue implementation. Failure is just propagated out.\n            //\n            switch (status)\n            {\n                case AsyncStatus.Error:\n                    error = info.ErrorCode ?? throw new InvalidOperationException(\"The asynchronous operation failed with a null error code.\");\n                    break;\n                case AsyncStatus.Canceled:\n                    error = new OperationCanceledException();\n                    break;\n                case AsyncStatus.Completed:\n                    if (_getResult != null)\n                    {\n                        result = _getResult(info);\n                    }\n\n                    break;\n                default:\n                    if (!initial)\n                    {\n                        throw new InvalidOperationException(\"The asynchronous operation completed unexpectedly.\");\n                    }\n\n                    _onCompleted(info, (iai, s) => Done(iai, s, false));","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Foundation/AsyncInfoToObservableBridge.cs#L31-L67","documentation":"Rx.NET's AsyncInfoToObservableBridge adapts WinRT IAsyncInfo objects to observables. When the underlying IAsyncInfo reports AsyncStatus.Error, the bridge propagates its ErrorCode as the failure; this InvalidOperationException is thrown when ErrorCode is null, i.e. the async operation claims to have failed but carries no error object, which indicates a rogue/buggy IAsyncInfo implementation.","triggerScenarios":"Calling ToObservable (or subscribing) on a custom/broken Windows Runtime IAsyncInfo implementation whose Status becomes AsyncStatus.Error while ErrorCode returns null.","commonSituations":"Interoperating with hand-rolled or third-party WinRT async components (often projected via C++/CX or custom winmd types) that violate the IAsyncInfo contract by failing without setting an error code.","solutions":["Fix or replace the non-conformant IAsyncInfo implementation so it sets a valid ErrorCode when completing with AsyncStatus.Error","Wrap subscription/ToTask calls in try-catch and handle InvalidOperationException as 'operation failed with unspecified error'","Log the operation ID/status and report the bug to the component vendor"],"exampleFix":"// before: custom async op that fails without error code\ninfo.ErrorCode == null // -> InvalidOperationException\n// after: implement IAsyncInfo correctly\npublic HResult ErrorCode => _exception != null ? _exception.HResult : new HResult(0);","handlingStrategy":"try-catch","validationCode":"if (asyncInfo.Status == AsyncStatus.Error && asyncInfo.ErrorCode == null) throw new InvalidOperationException(\"Rogue IAsyncInfo: failed without ErrorCode\");","typeGuard":"static bool HasValidErrorCode(IAsyncInfo info) => info.Status != AsyncStatus.Error || info.ErrorCode != null;","tryCatchPattern":"try { observable.Subscribe(...); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"null error code\")) { /* treat as unspecified failure */ }","preventionTips":["Use only well-formed WinRT async implementations (IAsyncAction/Operation factories, AsyncInfo.Run)","Test custom IAsyncInfo implementations against the full status contract","Log operation status and error code at boundaries"],"tags":["winrt","async","null-error-code","interop"],"backgroundTag":"unexpected-response-shape","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"}