I am catching exceptions to provide better information for the logs to make debugging easier in production. Does this code follow best practices, since I'm not really handling the error but just logging extra information? I was also considering creating a custom RazorException
, and throwing that instead. What do you think?
public string ParseFile(string templatePath, object model = null)
{
//some code removed for clarity
try
{
ITemplate template = _templateService.Resolve(templatePath, model);
ExecuteContext context = new ExecuteContext();
content = template.Run(context);
}
catch (TemplateCompilationException tcex) {
_logger.Fatal("Razor parse failed: check for syntax error", tcex);
throw;
}
catch (InvalidOperationException ex) {
_logger.Fatal("Razor parse failed: trying to compile layout on its own", ex);
throw;
}
catch (Exception ex) {
_logger.Fatal("Razor parse failed", ex);
throw;
}
return content;
}