Decoding Sandboxed Solutions Error Messages

So, you’ve built your web part for your SaaS Intranet, you’ve built some amazing solution that does all these whizzy things and it all works. But, but at some undeterminable point after this, execution stops with “Web Part Error: Sandboxed Code Execution Request Failed”. This is a generic error message which circumvents your try-catch. Not a very informative error message!

Lets bring in our good friend JavaScript to save SharePoint yet again! We will create a pop up in JS to show the error message. here’s the code:

string MyScript = @"<script language='javascript'>alert({0});</script>";
try
{
   //Your code
}
catch (Exception ex)
{
   if (!Page.IsClientScriptBlockRegistered("DebugScript")) 
   {
     MyScript = String.Format(MyScript, ex.ToString());
     Page.RegisterClientScriptBlock("DebugScript", MyScript);
   }
}

If this doesnt work use Page.ClientScript.RegisterStartupScript instead of IsClientScriptBlockRegistered

Or If you box has internet access download the Visual Studio 2010 SharePoint Power Tools!