{"record":{"id":"354352856adb8dc9","repo":"microsoft/aspire","slug":"message-cdp-error-code","errorCode":null,"errorMessage":"{message} (CDP error {code}).","messagePattern":"(.+?) \\(CDP error (.+?)\\)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Browsers/BrowserLogsCdpProtocol.cs","lineNumber":319,"sourceCode":"    {\n        return JsonSerializer.Deserialize(framePayload, jsonTypeInfo)\n            ?? throw new InvalidOperationException(\"Tracked browser protocol frame was empty.\");\n    }\n\n    private static void ThrowIfProtocolError(BrowserLogsCdpProtocolError? error)\n    {\n        if (error is null)\n        {\n            return;\n        }\n\n        var message = string.IsNullOrWhiteSpace(error.Message)\n            ? \"Unknown browser protocol error.\"\n            : error.Message;\n\n        if (error.Code is int code)\n        {\n            throw new InvalidOperationException($\"{message} (CDP error {code}).\");\n        }\n\n        throw new InvalidOperationException(message);\n    }\n}\n\ninternal readonly record struct BrowserLogsCdpProtocolMessageHeader(long? Id, string? Method, string? SessionId);\n\ninternal abstract record BrowserLogsCdpProtocolEvent(string Method, string? SessionId);\n\ninternal sealed record BrowserLogsConsoleApiCalledEvent(string? SessionId, BrowserLogsRuntimeConsoleApiCalledParameters Parameters)\n    : BrowserLogsCdpProtocolEvent(BrowserLogsCdpProtocol.RuntimeConsoleApiCalledMethod, SessionId);\n\ninternal sealed record BrowserLogsExceptionThrownEvent(string? SessionId, BrowserLogsExceptionThrownParameters Parameters)\n    : BrowserLogsCdpProtocolEvent(BrowserLogsCdpProtocol.RuntimeExceptionThrownMethod, SessionId);\n\ninternal sealed record BrowserLogsLoadingFailedEvent(string? SessionId, BrowserLogsLoadingFailedParameters Parameters)\n    : BrowserLogsCdpProtocolEvent(BrowserLogsCdpProtocol.NetworkLoadingFailedMethod, SessionId);","sourceCodeStart":301,"sourceCodeEnd":337,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Browsers/BrowserLogsCdpProtocol.cs#L301-L337","documentation":"This error is thrown by ThrowIfProtocolError when a Chrome DevTools Protocol (CDP) response contains an error payload. The library surfaces the browser's own error message, appending the numeric CDP error code when one is present. It converts a low-level protocol failure into a standard InvalidOperationException for the calling parse method.","triggerScenarios":"Calling ParseCreateTargetResponse, ParseAttachToTargetResponse, ParseGetTargetsResponse, ParseCommandAckResponse, or ParseCaptureScreenshotAsync paths when the browser replies to a CDP command with an error object that has a Message and an integer Code (e.g. target closed, unknown session, method not found).","commonSituations":"Browser crashed or navigated away mid-session; CDP WebSocket connected to the wrong target; a stale sessionId was reused after the tab was closed; sending a CDP method unsupported by the browser version.","solutions":["Inspect the embedded CDP error code and message to identify the failing browser operation","Verify the browser instance is still running and the target/session id is valid","Re-create the target/session (CreateTarget + AttachToTarget) before retrying the command","Upgrade the browser if the method is not supported by its CDP version"],"exampleFix":"// before: reusing a captured sessionId after tab close\nvar targets = ParseGetTargetsResponse(response);\nParseAttachToTargetResponse(AttachToTarget(staleSessionId));\n// after: resolve fresh targets and re-attach\nvar targets = ParseGetTargetsResponse(SendCommand(\"Target.getTargets\"));\nvar sessionId = ParseAttachToTargetResponse(SendCommand(\"Target.attachToTarget\", ...));","handlingStrategy":"try-catch","validationCode":"// Before issuing the CDP command, confirm the target/session is alive:\nvar targets = ParseGetTargetsResponse(SendCommand(\"Target.getTargets\"));\nbool targetAlive = targets.Any(t => t.TargetId == targetId);","typeGuard":null,"tryCatchPattern":"try { ParseAttachToTargetResponse(SendCommand(cmd)); }\ncatch (InvalidOperationException ex)\n{\n    logger.LogWarning(ex, \"CDP command failed\");\n    // re-create target/session and retry once\n}","preventionTips":["Always resolve fresh target ids instead of caching them across navigations","Re-attach sessions after tab close or navigation teardown","Log every CDP command/response pair for post-mortem of protocol errors"],"tags":["cdp","browser","protocol-error","invalid-operation"],"backgroundTag":"api-error-response","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}