{"record":{"id":"2e115ddda27d57d9","repo":"iOfficeAI/OfficeCLI","slug":"swap-not-supported-for-this-document-type","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.Add.cs","lineNumber":513,"sourceCode":"        {\n            var file = result.GetValue(swapFileArg)!;\n            var path1 = MsysPathHint.Restore(result.GetValue(swapPath1Arg)!)!;\n            var path2 = MsysPathHint.Restore(result.GetValue(swapPath2Arg)!)!;\n\n            if (TryResident(file.FullName, req =>\n            {\n                req.Command = \"swap\";\n                req.Args[\"path\"] = path1;\n                req.Args[\"to\"] = path2;\n            }, json) is {} rc) return rc;\n\n            using var handler = DocumentHandlerFactory.Open(file.FullName, editable: true);\n            var (p1, p2) = handler switch\n            {\n                OfficeCli.Handlers.PowerPointHandler ppt => ppt.Swap(path1, path2),\n                OfficeCli.Handlers.WordHandler word => word.Swap(path1, path2),\n                OfficeCli.Handlers.ExcelHandler excel => excel.Swap(path1, path2),\n                _ => throw new InvalidOperationException(\"swap not supported for this document type\")\n            };\n            var message = $\"Swapped {p1} <-> {p2}\";\n            if (json) Console.WriteLine(OutputFormatter.WrapEnvelopeText(message));\n            else Console.WriteLine(message);\n            NotifyWatch(handler, file.FullName, path1);\n            return 0;\n        }, json); });\n\n        return swapCommand;\n    }\n}\n","sourceCodeStart":495,"sourceCodeEnd":525,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/CommandBuilder.Add.cs#L495-L525","documentation":"Thrown by the `swap` command's handler switch when the opened document's handler is none of PowerPointHandler, WordHandler, or ExcelHandler. The default arm raises InvalidOperationException. In practice the three Office handlers cover all supported extensions, so this is reachable mainly if DocumentHandlerFactory.Open returns an unexpected handler type (e.g. a custom/future handler) for a file that passed the extension check elsewhere.","triggerScenarios":"`officecli swap file.<ext> /p1 /p2` where the file opens as a handler not in the three-way switch. Realistically: a misconfigured factory, a corrupted file mis-detected as a different format, or a new handler type added without updating swap's switch.","commonSituations":"A future handler type is registered but swap was not updated; the file extension was spoofed/renamed so the factory picks an unexpected handler; an internal test fixture returns a stub handler; a plugin-influenced open path yields a non-standard handler.","solutions":["Confirm the file is genuinely .docx/.pptx/.xlsx and not renamed; check the extension matches the content.","Ensure DocumentHandlerFactory.Open returns one of the three supported handlers for that file.","If you added a custom handler, extend the swap switch to cover it before calling swap on that type."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"// Only call swap on the three supported handler types.\nusing var h = DocumentHandlerFactory.Open(file, editable: true);\nif (h is not (PowerPointHandler or WordHandler or ExcelHandler))\n    throw new NotSupportedException($\"swap not supported for {h.GetType().Name}\");","typeGuard":"// Type guard: handler supports swap.\nstatic bool HandlerSupportsSwap(DocumentHandler h) =>\n    h is PowerPointHandler or WordHandler or ExcelHandler;","tryCatchPattern":null,"preventionTips":["Check the handler type (or file extension) before calling swap.","Keep swap's switch and the factory's supported extensions in sync when adding new handlers."],"tags":["swap","unsupported","document-type","handler"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}