{"record":{"id":"fa9d6f0da0edf3b8","repo":"Tichau/FileConverter","slug":"the-conversion-preset-must-be-valid-fa9d6f","errorCode":null,"errorMessage":"The conversion preset must be valid.","messagePattern":"The conversion preset must be valid\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Application/FileConverter/ConversionJobs/ConversionJob_ImageMagick.cs","lineNumber":62,"sourceCode":"            {\n                using (MagickImageCollection images = new MagickImageCollection())\n                {\n                    MagickReadSettings settings = new MagickReadSettings();\n                    settings.Density = new Density(1, 1);\n                    images.Read(this.InputFilePath);\n\n                    return images.Count;\n                }\n            }\n\n            return 1;\n        }\n\n        protected override void Convert()\n        {\n            if (this.ConversionPreset == null)\n            {\n                throw new Exception(\"The conversion preset must be valid.\");\n            }\n\n            this.CurrentOutputFilePathIndex = 0;\n\n            if (this.isInputFilePdf)\n            {\n                this.ConvertPdf();\n            }\n            else\n            {\n                this.pageCount = 1;\n                MagickReadSettings readSettings = new MagickReadSettings();\n\n                string inputExtension = System.IO.Path.GetExtension(this.InputFilePath).ToLowerInvariant();\n                switch (inputExtension)\n                {\n                    case \".avif\":\n                        // Explicitly set AVIF format for proper reading","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/Tichau/FileConverter/blob/6c157a411fb70fb7440d2e3b226042781fd55b15/Application/FileConverter/ConversionJobs/ConversionJob_ImageMagick.cs#L44-L80","documentation":"ConversionJob_ImageMagick.Convert() throws Exception when this.ConversionPreset is null before branching on isInputFilePdf (ConvertPdf() vs single-page MagickReadSettings path) and writing images via ImageMagick. This job is what ConversionJobFactory.Create returns for Pdf, Avif, Jpg, Png and Webp outputs, so it is a high-traffic converter; the guard protects the MagickNET calls that would NRE on a null preset. As elsewhere, the real ctor rejects null presets, so the throw is reachable via the parameterless design-mode ctor or direct/reflection Convert().","triggerScenarios":"ConversionJob_ImageMagick built via its parameterless ctor then Convert() invoked; or Convert() called via reflection on an instance whose preset is null. The factory requires a non-null preset, so the normal factory path cannot reach this guard.","commonSituations":"XAML design-time instantiation; tests calling Convert() directly; an image/PDF conversion pipeline that created the job before the preset was loaded; a serialization path that reconstituted the job without a preset.","solutions":["Create via new ConversionJob_ImageMagick(conversionPreset, inputFilePath) or ConversionJobFactory.Create.","Use PrepareConversion() + StartConversion() rather than calling Convert().","Early-return in design-time when ConversionPreset is null.","Ensure preset deserialization from Settings.user.xml succeeded before job creation."],"exampleFix":"// before\nvar job = new ConversionJob_ImageMagick();\njob.Convert(); // throws: preset null\n\n// after\nvar job = ConversionJobFactory.Create(conversionPreset, inputFilePath);\njob.PrepareConversion(outputFilePaths);\njob.StartConversion();","handlingStrategy":"validation","validationCode":"if (job == null || job.ConversionPreset == null)\n{\n    throw new InvalidOperationException(\n        \"ConversionJob_ImageMagick requires a non-null ConversionPreset; use ConversionJobFactory.Create.\");\n}\njob.StartConversion();","typeGuard":"private static bool HasValidPreset(ConversionJob job) => job != null && job.ConversionPreset != null;","tryCatchPattern":"try\n{\n    job.StartConversion();\n}\ncatch (Exception ex) when (ex.Message.Contains(\"conversion preset must be valid\"))\n{\n    logger.Error(ex, \"ImageMagick job created without a preset; rebuilding.\");\n    job = ConversionJobFactory.Create(LoadPreset(), inputPath);\n}","preventionTips":["Always use ConversionJobFactory.Create(preset, inputPath) for image/PDF outputs.","Never call Convert() directly; use StartConversion().","Deserialize and validate the preset from Settings.user.xml at startup.","Treat the parameterless ctor as design-time only."],"tags":["csharp","fileconverter","conversion-preset","null-check","defensive-guard","imagemagick","image","pdf"],"backgroundTag":null,"analyzedSha":"6c157a411fb70fb7440d2e3b226042781fd55b15","analyzedAt":"2026-08-13T15:04:08.307Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}