I seem to be doing a lot of Exception swallowing with Child Actions.
[ChildActionOnly]
[OutputCache(Duration = 1200, VaryByParam = "key;param")]
public ActionResult ChildPart(int key, string param)
{
try
{
var model = DoRiskyExceptionProneThing(key, param)
return View("_ChildPart", model);
}
catch (Exception ex)
{
// Log to elmah using a helper method
ErrorLog.LogError(ex, "Child Action Error ");
// return a pretty bit of HTML to avoid a whitescreen of death on the client
return View("_ChildActionOnlyError");
}
}
I feel like I'm cutting and pasting heaps of code, and with each cut an paste we all know a kitten is being drowned in angels tears.
Is there a better way to manage exceptions in child actions that would allow the rest of the screen to render appropriately?
You could create a CustomHandleError attribute based on Mvc's HandleError attribute, override the OnException method, do your logging and possibly return a custom view.
Then decorate any controllers and/or actions that you want to enable with this logic like so: