Permalink
Browse files

Better exception handling/logging

  • Loading branch information...
1 parent b79ce24 commit 2f61f73f3bbefb792e7297ab606b59a26e6fb1bc @kmees kmees committed Mar 22, 2013
Showing with 400 additions and 346 deletions.
  1. +0 −1 src/IronSharePoint.Framework/Console/ScriptResult.cs
  2. +14 −0 src/IronSharePoint.Framework/Diagnostics/IronCategoryDiagnosticsId.cs
  3. +75 −9 src/IronSharePoint.Framework/Diagnostics/IronDiagnosticService.cs
  4. +62 −0 src/IronSharePoint.Framework/Exceptions.cs
  5. +14 −0 src/IronSharePoint.Framework/IWrapperControl.cs
  6. +0 −1 src/IronSharePoint.Framework/IronConsole.cs
  7. +15 −25 src/IronSharePoint.Framework/IronHttpModule.cs
  8. +23 −49 src/IronSharePoint.Framework/IronRuntime.cs
  9. +4 −0 src/IronSharePoint.Framework/IronSharePoint.Framework.csproj
  10. +30 −50 src/IronSharePoint.Framework/IronWrapperControl.cs
  11. +45 −87 src/IronSharePoint.Framework/IronWrapperUserControl.cs
  12. +7 −0 src/IronSharePoint.Framework/Util/EnumerableExtensions.cs
  13. +6 −1 src/IronSharePoint.Framework/Util/ScriptEngineExtensions.cs
  14. +34 −0 src/IronSharePoint.Framework/Util/WrapperControlExtensions.cs
  15. +53 −90 src/IronSharePoint/IronPart/IronPart.cs
  16. +2 −1 src/IronSharePoint/IronSharePoint.csproj
  17. +1 −2 src/IronSharePoint/Layouts/IronSP/IronConsole.coffee
  18. +1 −2 src/IronSharePoint/Layouts/IronSP/IronConsole.js
  19. +3 −4 src/IronSharePoint/Layouts/IronSP/IronConsoleService.ashx.cs
  20. +0 −5 src/IronSharePoint/TEMPLATE/Features/IronSP_Root/iron_sharepoint/iron_composite_control.rb
  21. +0 −5 src/IronSharePoint/TEMPLATE/Features/IronSP_Root/iron_sharepoint/iron_control.rb
  22. +0 −2 src/IronSharePoint/TEMPLATE/Features/IronSP_Root/iron_sharepoint/main_http_handler.rb
  23. +0 −9 src/IronSharePoint/TEMPLATE/Features/IronSP_Root/iron_sharepoint/mixins/type_registration.rb
  24. +7 −0 src/IronSharePoint/TEMPLATE/Features/IronSP_Root/iron_sharepoint/patches/system/date_time.rb
  25. +0 −1 test/IronSharePoint.Framework.Test/Console/ScriptResult_Fixture.cs
  26. +3 −1 test/IronSharePoint.Framework.Test/Util/ScriptEngineExtensions_Fixture.cs
  27. +1 −1 test/IronSharePoint.Framework.Test/_assets/script_result.json
@@ -8,7 +8,6 @@ public class ScriptResult
public string Output { get; set; }
public string ReturnValue { get; set; }
public string Error { get; set; }
- public string StackTrace { get; set; }
public long ExecutionTime { get; set; }
public bool HasError
@@ -0,0 +1,14 @@
+namespace IronSharePoint.Diagnostics
+{
+ public enum IronCategoryDiagnosticsId
+ {
+ None = 0,
+ Core = 100,
+ Controls = 200,
+ WebParts = 300,
+ EventReceivers = 400,
+ Jobs = 900,
+ Services = 1000,
+ Unknown = 9999
+ }
+}
@@ -2,21 +2,87 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
+using Microsoft.Scripting.Hosting;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
namespace IronSharePoint.Diagnostics
{
- public enum IronCategoryDiagnosticsId
+ public class IronULSLogger
{
- None = 0,
- Core = 100,
- Controls = 200,
- WebParts = 300,
- EventReceivers = 400,
- Jobs = 900,
- Services = 1000,
- Unknown = 9999
+ private readonly IronDiagnosticsService _service;
+
+ const string ErrorFormat = "Exception '{0}' - {1} at {2}";
+
+ public IronULSLogger(IronDiagnosticsService service)
+ {
+ _service = service;
+ }
+
+ public IronULSLogger() : this(IronDiagnosticsService.Local)
+ {
+ }
+
+ static readonly Lazy<IronULSLogger> _local = new Lazy<IronULSLogger>(() => new IronULSLogger(), true);
+
+ public static IronULSLogger Local
+ {
+ get { return _local.Value; }
+ }
+
+ public void Verbose(string message, IronCategoryDiagnosticsId categoryId)
+ {
+ IronDiagnosticsService.Local.WriteTrace(1, IronDiagnosticsService.Local[categoryId],
+ TraceSeverity.Verbose, message);
+ }
+
+ public void Error(string message, IronCategoryDiagnosticsId categoryId = IronCategoryDiagnosticsId.None)
+ {
+ _service.WriteTrace(1, IronDiagnosticsService.Local[categoryId], TraceSeverity.Unexpected, message);
+ }
+
+ public string Error(string message, Exception ex, IronCategoryDiagnosticsId categoryId = IronCategoryDiagnosticsId.None)
+ {
+ string output;
+ if (!IsRubyException(ex) || !TryFormatRubyException(ex, out output))
+ {
+ output = String.Format(ErrorFormat,
+ ex.GetType().Name,
+ ex.Message,
+ ex.StackTrace);
+ }
+ if (!String.IsNullOrWhiteSpace(message)) output = message + " - " + output;
+ _service.WriteTrace(1, IronDiagnosticsService.Local[categoryId], TraceSeverity.Unexpected, output);
+
+ return output;
+ }
+
+ bool IsRubyException(Exception ex)
+ {
+ return ex.TargetSite != null
+ && ex.TargetSite.DeclaringType.FullName.StartsWith("Microsoft.Scripting.Interpreter");
+ }
+
+ bool TryFormatRubyException(Exception ex, out string output)
+ {
+ output = string.Empty;
+ if (SPContext.Current != null)
+ {
+ try
+ {
+ var runtime = IronRuntime.GetDefaultIronRuntime(SPContext.Current.Site);
+ var expcetionOperations = runtime.RubyEngine.GetService<ExceptionOperations>();
+
+ output = expcetionOperations.FormatException(ex);
+ return true;
+ }
+ catch
+ {
+ return false;
+ }
+ }
+ return false;
+ }
}
[System.Runtime.InteropServices.GuidAttribute("8D5F89BA-0DE0-4D46-811C-E631B6FAC228")]
@@ -0,0 +1,62 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.Serialization;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace IronSharePoint.Exceptions
+{
+ [Serializable]
+ public class IronRuntimeAccesssException : Exception
+ {
+ public Guid SiteId { get; set; }
+
+ public IronRuntimeAccesssException() {}
+
+ public IronRuntimeAccesssException(string message)
+ : base(message) {}
+
+ public IronRuntimeAccesssException(string message, Exception inner)
+ : base(message, inner) {}
+
+ protected IronRuntimeAccesssException(
+ SerializationInfo info,
+ StreamingContext context)
+ : base(info, context) {}
+ }
+
+ [Serializable]
+ public class RubyFrameworkInitializationException : Exception
+ {
+ public RubyFrameworkInitializationException() {}
+
+ public RubyFrameworkInitializationException(string message)
+ : base(message) {}
+
+ public RubyFrameworkInitializationException(string message, Exception inner)
+ : base(message, inner) {}
+
+ protected RubyFrameworkInitializationException(
+ SerializationInfo info,
+ StreamingContext context)
+ : base(info, context) {}
+ }
+
+ [Serializable]
+ public class DynamicInstanceInitializationException : Exception
+ {
+ public DynamicInstanceInitializationException() {}
+
+ public DynamicInstanceInitializationException(string message)
+ : base(message) {}
+
+ public DynamicInstanceInitializationException(string message, Exception inner)
+ : base(message, inner) {}
+
+ protected DynamicInstanceInitializationException(
+ SerializationInfo info,
+ StreamingContext context)
+ : base(info, context) {}
+ }
+}
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace IronSharePoint
+{
+ interface IWrapperControl
+ {
+ string ControlName { get; set; }
+ Exception InstantiationException { get; set; }
+ }
+}
@@ -54,7 +54,6 @@ private ScriptResult RunScript(string script)
catch (Exception ex)
{
result.Error = ex.Message;
- result.StackTrace = ex.StackTrace;
}
sw.Stop();
result.ExecutionTime = sw.ElapsedMilliseconds;
@@ -1,50 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
-using System.Text;
using System.Web;
-using Microsoft.SharePoint;
-using Microsoft.Scripting.Hosting;
+using IronSharePoint.Diagnostics;
namespace IronSharePoint
{
- public class IronHttpModule:IHttpModule
+ public class IronHttpModule : IHttpModule
{
public void Init(HttpApplication application)
{
- application.EndRequest += new EventHandler(EndRequest);
- application.Error += new EventHandler(Error);
+ application.EndRequest += EndRequest;
+ application.Error += Error;
}
- void Error(object sender, EventArgs e)
+ public void Dispose()
{
- var application = sender as HttpApplication;
-
- if (SPContext.Current != null)
- {
- var runtime = IronRuntime.GetDefaultIronRuntime(SPContext.Current.Site);
- var engine = runtime.RubyEngine;
- var exception = application.Server.GetLastError();
+ }
- var eo = engine.GetService<ExceptionOperations>();
- string error = eo.FormatException(exception);
+ private void Error(object sender, EventArgs e)
+ {
+ var application = sender as HttpApplication;
- IronRuntime.LogError(error, exception);
- }
+ IronULSLogger.Local.Error("Error in HttpModule", application.Server.GetLastError(), IronCategoryDiagnosticsId.Core);
}
-
- void EndRequest(object sender, EventArgs e)
+
+ private void EndRequest(object sender, EventArgs e)
{
var application = sender as HttpApplication;
- CleanUp(application);
+ CleanUp(application);
}
private static void CleanUp(HttpApplication application)
{
var ironObjectsToDispose = new List<IDisposable>();
- foreach (var key in application.Context.Items.Keys.OfType<String>())
+ foreach (string key in application.Context.Items.Keys.OfType<String>())
{
if (key.StartsWith(IronConstant.IronPrefix))
{
@@ -58,7 +50,5 @@ private static void CleanUp(HttpApplication application)
ironObjectsToDispose.ForEach(o => o.Dispose());
}
-
- public void Dispose() { }
}
-}
+}
Oops, something went wrong.

0 comments on commit 2f61f73

Please sign in to comment.