Skip to content

Instantly share code, notes, and snippets.

@jmarnold
Created November 27, 2012 16:02
Show Gist options
  • Save jmarnold/4155080 to your computer and use it in GitHub Desktop.
Save jmarnold/4155080 to your computer and use it in GitHub Desktop.
Ajax continuation failures
public static AjaxContinuation SystemFailure(this AjaxContinuation continuation, Exception exception)
{
continuation.Success = false;
continuation[EXCEPTION] = new AjaxException
{
type = exception.GetType().Name,
message = exception.Message,
stacktrace = exception.StackTrace
};
return continuation;
}
public class AjaxException
{
public string type { get; set; }
public string message { get; set; }
public string stacktrace { get; set; }
}
public class ReportSystemFailure : IActionBehavior
{
private readonly IActionBehavior _inner;
private readonly IJsonWriter _writer;
private readonly ILogger _logger;
public ReportSystemFailure(IActionBehavior inner, IJsonWriter writer, ILogger logger)
{
_inner = inner;
_writer = writer;
_logger = logger;
}
public void Invoke()
{
try
{
_inner.Invoke();
}
catch (Exception exc)
{
_logger.Error(exc.Message, exc);
var continuation = new AjaxContinuation();
continuation.SystemFailure(exc);
_writer.Write(continuation.ToDictionary(), MimeType.Json.ToString());
}
}
public void InvokePartial()
{
Invoke();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment