{"record":{"id":"e466baa8b733230d","repo":"iOfficeAI/OfficeCLI","slug":"swap-not-supported-for-this-document-type-e466ba","errorCode":null,"errorMessage":"swap not supported for this document type","messagePattern":"swap not supported for this document type","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/officecli/CommandBuilder.cs","lineNumber":1176,"sourceCode":"                var resultPath = handler.Move(path, item.To, movePos, props.Count > 0 ? props : null);\n                return $\"Moved to {resultPath}\";\n            }\n            case \"swap\":\n            {\n                // Second element: accept `path2` (canonical — the single-command\n                // MCP tool and the CLI `swap path1 path2` both use it) or the\n                // legacy `to`. Before path2 was carried, an agent that learned\n                // swap from the single command produced a batch item that\n                // silently failed the path-presence check below.\n                var swapTo = !string.IsNullOrEmpty(item.Path2) ? item.Path2 : item.To;\n                if (string.IsNullOrEmpty(item.Path) || string.IsNullOrEmpty(swapTo))\n                    throw new ArgumentException(\"'swap' command requires 'path' and 'path2' (or 'to') fields. Example: {\\\"command\\\": \\\"swap\\\", \\\"path\\\": \\\"/slide[1]\\\", \\\"path2\\\": \\\"/slide[2]\\\"}\");\n                var (p1, p2) = handler switch\n                {\n                    OfficeCli.Handlers.PowerPointHandler ppt => ppt.Swap(item.Path, swapTo),\n                    OfficeCli.Handlers.WordHandler word => word.Swap(item.Path, swapTo),\n                    OfficeCli.Handlers.ExcelHandler excel => excel.Swap(item.Path, swapTo),\n                    _ => throw new InvalidOperationException(\"swap not supported for this document type\")\n                };\n                return $\"Swapped {p1} <-> {p2}\";\n            }\n            case \"view\":\n            {\n                var mode = item.Mode ?? \"text\";\n                if (mode.ToLowerInvariant() is \"html\" or \"h\")\n                {\n                    if (handler is OfficeCli.Handlers.PowerPointHandler pptH)\n                        return pptH.ViewAsHtml();\n                    if (handler is OfficeCli.Handlers.ExcelHandler excelH)\n                        return excelH.ViewAsHtml();\n                    if (handler is OfficeCli.Handlers.WordHandler wordH)\n                        return wordH.ViewAsHtml();\n                }\n                if (mode.ToLowerInvariant() is \"svg\" or \"g\" && handler is OfficeCli.Handlers.PowerPointHandler pptSvg)\n                {\n                    return pptSvg.ViewAsSvg(1);","sourceCodeStart":1158,"sourceCodeEnd":1194,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/CommandBuilder.cs#L1158-L1194","documentation":"Thrown by the batch 'swap' command when the active document handler is neither PowerPointHandler, WordHandler, nor ExcelHandler. Swap is only implemented for those three document types; any other handler type falls to the default switch arm. It is an InvalidOperationException (not a CliException) because it reflects a capability gap, not malformed input.","triggerScenarios":"Running a batch that contains a 'swap' item against a document type whose handler has no Swap overload. In practice this is rare since the three main handlers are covered, but it fires for any future/custom handler.","commonSituations":"A pipeline that dispatches the same batch JSON across multiple file types, where one type lacks swap support. A test harness using a mock/stub handler.","solutions":["Confirm the document is a .pptx, .docx, or .xlsx — only those support swap.","If you must reorder elements in an unsupported type, use 'move' (move to index/before/after) instead of swap.","Drop the swap item from batches sent to unsupported document types."],"exampleFix":"// before — swap item on an unsupported handler\n{\"command\":\"swap\",\"path\":\"/a[1]\",\"path2\":\"/a[2]\"}\n// after — reorder with move instead\n{\"command\":\"move\",\"path\":\"/a[1]\",\"to\":\"/a\",\"index\":2}","handlingStrategy":"type-guard","validationCode":"// only issue swap for handlers that implement it\nvar swapCapable = handler is PowerPointHandler or WordHandler or ExcelHandler;\nif (item.Command == \"swap\" && !swapCapable)\n    throw new InvalidOperationException(\"swap is not supported for this document type; use 'move' instead\");","typeGuard":"static bool HandlerSupportsSwap(object handler) =>\n    handler is PowerPointHandler or WordHandler or ExcelHandler;","tryCatchPattern":"try { (p1, p2) = DispatchSwap(handler, item); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"swap not supported\"))\n{ /* fall back to 'move' or skip the item */ }","preventionTips":["Gate swap items on the handler type before dispatch.","Keep type-specific operations out of generic cross-format batch templates.","For unsupported types, substitute 'move' with an explicit index."],"tags":["batch","swap","unsupported","handler","document-type"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}