Skip to content

Instantly share code, notes, and snippets.

@jmarnold
Created July 2, 2011 15:16
Show Gist options
  • Save jmarnold/1060742 to your computer and use it in GitHub Desktop.
Save jmarnold/1060742 to your computer and use it in GitHub Desktop.
ActionLess JSON
public class JsonResponse
{
private readonly IList<string> _errors = new List<string>();
public bool Success { get; set; }
public IEnumerable<string> Errors { get { return _errors; } }
public void RegisterError(string error)
{
_errors.Fill(error);
}
}
public class SimpleErrorHandlingBehavior : IActionBehavior
{
private readonly IActionBehavior _inner;
private readonly IFubuRequest _request;
private readonly IPartialFactory _factory;
public SimpleErrorHandlingBehavior(IActionBehavior inner, IFubuRequest request, IPartialFactory factory)
{
_inner = inner;
}
public void Invoke()
{
if(_inner == null)
{
return;
}
try
{
_inner.Invoke();
}
catch(Exception exc)
{
var response = new JsonResponse { Success = false };
response.RegisterError(exc.Message);
_request.Set(response);
_factory
.BuildPartial(response.GetType())
.InvokePartial();
}
}
}
public class UselessAction
{
public JsonResponse Execute(JsonResponse response)
{
return response;
}
}
@ryankelley
Copy link

You say you don't put code like the SimpleErrorHandlingBehavior in production, so what do you use for this type of scenario?

@jmarnold
Copy link
Author

jmarnold commented Aug 1, 2011

@ryankelley I typically have more finer grained control over my exceptions and a lot of my actioncalls are really only performing validation. Commands are then invoked which are designed not to fail so this isn't a common approach. That being said, this is a nifty behavior to have for a "catch all" scenario for AJAX requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment