Browse Source

Catch all exceptions thrown when processing a t4 template.

pull/16/merge
Matt Ward 15 years ago
parent
commit
c9cb9ab036
  1. 1
      src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingCustomToolContext.cs
  2. 6
      src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingCustomToolContext.cs
  3. 13
      src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingFileGenerator.cs
  4. 8
      src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingCustomToolContext.cs
  5. 40
      src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingFileGeneratorTests.cs

1
src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingCustomToolContext.cs

@ -14,5 +14,6 @@ namespace ICSharpCode.TextTemplating @@ -14,5 +14,6 @@ namespace ICSharpCode.TextTemplating
void ClearTasksExceptCommentTasks();
void AddTask(Task task);
void BringErrorsPadToFront();
void DebugLog(string message, Exception ex);
}
}

6
src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingCustomToolContext.cs

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Project;
@ -36,5 +37,10 @@ namespace ICSharpCode.TextTemplating @@ -36,5 +37,10 @@ namespace ICSharpCode.TextTemplating
{
WorkbenchSingleton.Workbench.GetPad(typeof(ErrorListPad)).BringPadToFront();
}
public void DebugLog(string message, Exception ex)
{
LoggingService.DebugFormatted("{0}\r\n{1}", message, ex.ToString());
}
}
}

13
src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingFileGenerator.cs

@ -55,12 +55,18 @@ namespace ICSharpCode.TextTemplating @@ -55,12 +55,18 @@ namespace ICSharpCode.TextTemplating
return TryProcessingTemplate(inputFileName, outputFileName);
}
string GetOutputFileName(string inputFileName)
{
return Path.ChangeExtension(inputFileName, ".cs");
}
bool TryProcessingTemplate(string inputFileName, string outputFileName)
{
try {
return host.ProcessTemplate(inputFileName, outputFileName);
} catch (InvalidOperationException ex) {
} catch (Exception ex) {
AddCompilerErrorToTemplatingHost(ex, inputFileName);
DebugLogException(ex, inputFileName);
}
return false;
}
@ -71,9 +77,10 @@ namespace ICSharpCode.TextTemplating @@ -71,9 +77,10 @@ namespace ICSharpCode.TextTemplating
host.Errors.Add(error);
}
string GetOutputFileName(string inputFileName)
void DebugLogException(Exception ex, string fileName)
{
return Path.ChangeExtension(inputFileName, ".cs");
string message = String.Format("Exception thrown when processing template '{0}'.", fileName);
context.DebugLog(message, ex);
}
void AddAnyErrorsToTaskList()

8
src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingCustomToolContext.cs

@ -16,6 +16,8 @@ namespace TextTemplating.Tests.Helpers @@ -16,6 +16,8 @@ namespace TextTemplating.Tests.Helpers
public bool IsOutputFileNamePassedToEnsureOutputFileIsInProject;
public bool IsClearTasksExceptCommentTasksCalled;
public bool IsBringErrorsPadToFrontCalled;
public Exception ExceptionPassedToDebugLog;
public string MessagePassedToDebugLog;
public List<Task> TasksAdded = new List<Task>();
@ -50,5 +52,11 @@ namespace TextTemplating.Tests.Helpers @@ -50,5 +52,11 @@ namespace TextTemplating.Tests.Helpers
{
IsBringErrorsPadToFrontCalled = true;
}
public void DebugLog(string message, Exception ex)
{
MessagePassedToDebugLog = message;
ExceptionPassedToDebugLog = ex;
}
}
}

40
src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingFileGeneratorTests.cs

@ -172,5 +172,45 @@ namespace TextTemplating.Tests @@ -172,5 +172,45 @@ namespace TextTemplating.Tests
Assert.IsFalse(customToolContext.IsBringErrorsPadToFrontCalled);
}
[Test]
public void ProcessTemplate_ThrowsInvalidOperationException_ExceptionLogged()
{
CreateGenerator(@"d:\a.tt");
var ex = new InvalidOperationException("invalid operation error");
templatingHost.ExceptionToThrowWhenProcessTemplateCalled = ex;
generator.ProcessTemplate();
Exception exceptionLogged = customToolContext.ExceptionPassedToDebugLog;
Assert.AreEqual(ex, exceptionLogged);
}
[Test]
public void ProcessTemplate_ThrowsInvalidOperationException_MessageLoggedIncludesInformationAboutProcessingTemplateFailure()
{
CreateGenerator(@"d:\a.tt");
var ex = new InvalidOperationException("invalid operation error");
templatingHost.ExceptionToThrowWhenProcessTemplateCalled = ex;
generator.ProcessTemplate();
string message = customToolContext.MessagePassedToDebugLog;
string expectedMessage = "Exception thrown when processing template 'd:\\a.tt'.";
Assert.AreEqual(expectedMessage, message);
}
[Test]
public void ProcessTemplate_ThrowsException_ErrorTaskAddedToTaskList()
{
CreateGenerator(@"d:\a.tt");
var ex = new Exception("error");
templatingHost.ExceptionToThrowWhenProcessTemplateCalled = ex;
generator.ProcessTemplate();
Task task = customToolContext.FirstTaskAdded;
Assert.AreEqual("error", task.Description);
}
}
}
Loading…
Cancel
Save