Magnus Lindhe
Magnus Lindhe

Reputation: 7317

What is the proper way of aborting a build using Cake?

Using the Error() method only logs an error. But what if I want to abort the build? I can throw an exception to abort the build but it does not seem right. So is there a proper way to abort?

var releaseNotes = ParseReleaseNotes("./ReleaseNotes.md");

if(releaseNotes.Version.ToString() != nugetVersion)
{
    Error("Release notes are missing an entry for v{0}. Latest release notes are for v{1}", nugetVersion, releaseNotes.Version);
    throw new Exception();
}

Upvotes: 3

Views: 911

Answers (2)

Patrik Svensson
Patrik Svensson

Reputation: 13844

The Error method is just a convenience method for logging an error. I understand the confusion though.

If something is wrong with that you cannot recover from, you should throw an exception to indicate it. The Cake script runner will then log the error (using the Error method) and return exit code 1 to indicate that something went wrong.

Upvotes: 3

Magnus Lindhe
Magnus Lindhe

Reputation: 7317

Ok, so the answer is to throw an exception and not use the Error() method like this:

var releaseNotes = ParseReleaseNotes("./ReleaseNotes.md");

if(releaseNotes.Version.ToString() != nugetVersion)
{
    throw new Exception("Release notes are missing an entry for v{0}. Latest release notes are for v{1}", nugetVersion, releaseNotes.Version);        
}

Upvotes: 1

Related Questions