HiveBrain v1.2.0
Get Started
← Back to all entries
debugcsharpModerate

Console application for providing detailed error messages

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
errorapplicationdetailedprovidingmessagesforconsole

Problem

I'm writing a console application where I need to provide detailed error messages in case anything goes wrong. As a result of this, I find myself splitting up my code into many pieces and making sure that every step is handled correctly. While this certainly makes it possible to detail what exactly went wrong, it unfortunately also leads some horrific overhead on my code. Take the following code for example:

// Open the SISS catalog on the server
        Catalog catalog = integrationServices.Catalogs[catalogName];
        if (catalog == null)
        {
            Console.WriteLine("Unable to open the SSIS catalog : " + catalogName 
                + ", it does not exist on the server");
            return (int)ExitCode.Failure;
        }

        // Open the folder in the catalog
        CatalogFolder folder = catalog.Folders[folderName];
        if (folder == null)
        {
            Console.WriteLine("Unable to open the folder : " + folderName
                + ", it does not exist on the server");
            return (int)ExitCode.Failure;
        }

        // Open the project in the folder
        ProjectInfo projectInfo = folder.Projects[projectName];
        if (projectInfo == null)
        {
            Console.WriteLine("Unable to open the project : " + projectName 
                + ", it does not exist on the server");
            return (int)ExitCode.Failure;
        }

        // Check if the package exists
        if (!projectInfo.Packages.Contains(packageName))
        {
            Console.WriteLine("Unable to open the package : " + packageName
                + ", it does not exist on the server");
            return (int)ExitCode.Failure;
        }


Is there any way I can make this more pretty?

Solution

Make a function that writes an error message (String) to the console, then exits with failure error code. But I'm not sure whether this would mess up your returns and overall control flow.

Other than that, I don't know. Yes, you can golf the string to "Unable to open X : " + x + ", it does not exist on the server", but all you're doing is making the error message harder to find when debugging later.

That said, "pretty" is pretty (Hah) abstract. As it is now, the code is perfectly readable, and it doesn't contain too much duplication. It doesn't contain any performance issues either. What you're seeing here is your code reaching the lowest Kolmogorov Complexity it can.

But let's say, for the heck of it, you were to create some sort of function that takes a catalogName and returns either null or a Catalog object. In case of null, you have to call another function to get the error message. If you did that, you're just relocating the if statements. It also makes the design confusing. Basically, I don't think you can because you have to check and you have to report. In my opinion, you did that the prettiest way possible.

Context

StackExchange Code Review Q#58612, answer score: 12

Revisions (0)

No revisions yet.