Created
July 2, 2011 15:16
-
-
Save jmarnold/1060742 to your computer and use it in GitHub Desktop.
ActionLess JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UselessAction | |
{ | |
public JsonResponse Execute(JsonResponse response) | |
{ | |
return response; | |
} | |
} |
@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
You say you don't put code like the SimpleErrorHandlingBehavior in production, so what do you use for this type of scenario?