Skip to content

Instantly share code, notes, and snippets.

@KevM
Created June 22, 2010 21:02
Show Gist options
  • Select an option

  • Save KevM/449070 to your computer and use it in GitHub Desktop.

Select an option

Save KevM/449070 to your computer and use it in GitHub Desktop.
public abstract class InterceptExceptionBehavior<T> : IActionBehavior
where T : Exception
{
public IActionBehavior InsideBehavior { get; set; }
public void InvokePartial()
{
if (InsideBehavior != null)
InsideBehavior.InvokePartial();
}
public void Invoke()
{
if (InsideBehavior == null)
throw new FubuAssertionException("When interception exceptions you must have an inside behavior. Otherwise, there would be nothing to intercept.");
try
{
InsideBehavior.Invoke();
}
catch (T exception)
{
if (!ShouldHandle(exception))
throw;
Handle(exception);
}
}
public virtual bool ShouldHandle(T exception)
{
return true;
}
public abstract void Handle(T exception);
}
public class LuceneStatusExceptionBehavior : InterceptExceptionBehavior<Exception>
{
private readonly IOutputWriter _outputWriter;
private readonly IHttpResponseStatus _httpResponseStatus;
public LuceneStatusExceptionBehavior(IOutputWriter outputWriter, IHttpResponseStatus httpResponseStatus)
{
_outputWriter = outputWriter;
_httpResponseStatus = httpResponseStatus;
}
public override bool ShouldHandle(Exception exception)
{
return exception.GetType().FullName.Contains("Lucene");
}
public override void Handle(Exception exception)
{
_httpResponseStatus.Code = 400;
var htmlDocument = getHttpStatusErrorDocument(exception, _httpResponseStatus.Code);
_outputWriter.Write("text/html", htmlDocument.ToString());
}
private static HtmlDocument getHttpStatusErrorDocument(Exception exception, int httpStatusCode)
{
var title = "Status " + httpStatusCode;
var doc = new HtmlDocument {Title = title};
var content = doc.Add("div").AddClass("content");
content.AddChildren(
new[]
{
new HtmlTag("h1").Text(title),
new HtmlTag("p").Text("Your Dovetail Seeker search API request failed."),
new HtmlTag("h2").Text("Reason"),
new HtmlTag("p").Text(exception.Message)
});
return doc;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment