{"record":{"id":"a87530e3919f4c2a","repo":"spectreconsole/spectre.console","slug":"cannot-export-html-since-a-recording-hasn-t-been-s","errorCode":null,"errorMessage":"Cannot export HTML since a recording hasn't been started.","messagePattern":"Cannot export HTML since a recording hasn't been started\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Spectre.Console/AnsiConsole.Recording.cs","lineNumber":41,"sourceCode":"    public static string ExportText()\n    {\n        if (_recorder == null)\n        {\n            throw new InvalidOperationException(\"Cannot export text since a recording hasn't been started.\");\n        }\n\n        return _recorder.ExportText();\n    }\n\n    /// <summary>\n    /// Exports all recorded console output as HTML text.\n    /// </summary>\n    /// <returns>The recorded output as HTML text.</returns>\n    public static string ExportHtml()\n    {\n        if (_recorder == null)\n        {\n            throw new InvalidOperationException(\"Cannot export HTML since a recording hasn't been started.\");\n        }\n\n        return _recorder.ExportHtml();\n    }\n\n    /// <summary>\n    /// Exports all recorded console output using a custom encoder.\n    /// </summary>\n    /// <param name=\"encoder\">The encoder to use.</param>\n    /// <returns>The recorded output.</returns>\n    public static string ExportCustom(IAnsiConsoleEncoder encoder)\n    {\n        if (_recorder == null)\n        {\n            throw new InvalidOperationException(\"Cannot export HTML since a recording hasn't been started.\");\n        }\n\n        ArgumentNullException.ThrowIfNull(encoder);","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/spectreconsole/spectre.console/blob/0acc92fada6c42f13984e79c2b5f3d993bdfb099/src/Spectre.Console/AnsiConsole.Recording.cs#L23-L59","documentation":"Thrown by AnsiConsole.ExportHtml() when no Recorder is attached, i.e. AnsiConsole.Record() was never called. The HTML exporter needs captured console writes, so exporting without recording is an invalid state (InvalidOperationException).","triggerScenarios":"Calling AnsiConsole.ExportHtml() before AnsiConsole.Record().","commonSituations":"Forgot to call Record(); a code path exports HTML in a branch where recording never started; reordered setup after a refactor; test asserting HTML export without setup.","solutions":["Call AnsiConsole.Record() before the output you want captured, then ExportHtml()","Track recording state yourself and guard the export call","Wrap ExportHtml() in try/catch(InvalidOperationException) if recording is optional"],"exampleFix":"// before\nAnsiConsole.WriteLine(\"hello\");\nvar html = AnsiConsole.ExportHtml(); // throws\n\n// after\nAnsiConsole.Record();\nAnsiConsole.WriteLine(\"hello\");\nvar html = AnsiConsole.ExportHtml();","handlingStrategy":"try-catch","validationCode":"// No public API exposes recording state; track it locally\nbool recording = false;\nAnsiConsole.Record();\nrecording = true;\nAnsiConsole.WriteLine(\"hello\");\nif (recording)\n    var html = AnsiConsole.ExportHtml();","typeGuard":null,"tryCatchPattern":"try\n{\n    var html = AnsiConsole.ExportHtml();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"recording hasn't been started\"))\n{\n    // recording was never started; handle gracefully\n}","preventionTips":["Always call AnsiConsole.Record() before the output you intend to capture","Mirror recording state in a local bool","Centralize start/export in one helper"],"tags":["recording","state","usage","export","html"],"backgroundTag":null,"analyzedSha":"0acc92fada6c42f13984e79c2b5f3d993bdfb099","analyzedAt":"2026-08-13T18:27:21.983Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}