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. 15
      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
void ClearTasksExceptCommentTasks(); void ClearTasksExceptCommentTasks();
void AddTask(Task task); void AddTask(Task task);
void BringErrorsPadToFront(); void BringErrorsPadToFront();
void DebugLog(string message, Exception ex);
} }
} }

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

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

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

@ -55,12 +55,18 @@ namespace ICSharpCode.TextTemplating
return TryProcessingTemplate(inputFileName, outputFileName); return TryProcessingTemplate(inputFileName, outputFileName);
} }
string GetOutputFileName(string inputFileName)
{
return Path.ChangeExtension(inputFileName, ".cs");
}
bool TryProcessingTemplate(string inputFileName, string outputFileName) bool TryProcessingTemplate(string inputFileName, string outputFileName)
{ {
try { try {
return host.ProcessTemplate(inputFileName, outputFileName); return host.ProcessTemplate(inputFileName, outputFileName);
} catch (InvalidOperationException ex) { } catch (Exception ex) {
AddCompilerErrorToTemplatingHost(ex, inputFileName); AddCompilerErrorToTemplatingHost(ex, inputFileName);
DebugLogException(ex, inputFileName);
} }
return false; return false;
} }
@ -71,11 +77,12 @@ namespace ICSharpCode.TextTemplating
host.Errors.Add(error); 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() void AddAnyErrorsToTaskList()
{ {
foreach (CompilerError error in host.Errors) { foreach (CompilerError error in host.Errors) {

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

@ -16,6 +16,8 @@ namespace TextTemplating.Tests.Helpers
public bool IsOutputFileNamePassedToEnsureOutputFileIsInProject; public bool IsOutputFileNamePassedToEnsureOutputFileIsInProject;
public bool IsClearTasksExceptCommentTasksCalled; public bool IsClearTasksExceptCommentTasksCalled;
public bool IsBringErrorsPadToFrontCalled; public bool IsBringErrorsPadToFrontCalled;
public Exception ExceptionPassedToDebugLog;
public string MessagePassedToDebugLog;
public List<Task> TasksAdded = new List<Task>(); public List<Task> TasksAdded = new List<Task>();
@ -50,5 +52,11 @@ namespace TextTemplating.Tests.Helpers
{ {
IsBringErrorsPadToFrontCalled = true; 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
Assert.IsFalse(customToolContext.IsBringErrorsPadToFrontCalled); 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