How to create a fubumvc behavior that transfers actions with a certain type of return value, and if an exception occurs during the execution of an action, then the behavior registers an exception and fills some fields in the returned object? I tried the following:
public class JsonExceptionHandlingBehaviour : IActionBehavior
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
private readonly IActionBehavior _innerBehavior;
private readonly IFubuRequest _request;
public JsonExceptionHandlingBehaviour(IActionBehavior innerBehavior, IFubuRequest request)
{
_innerBehavior = innerBehavior;
_request = request;
}
public void Invoke()
{
try
{
_innerBehavior.Invoke();
var response = _request.Get<AjaxResponse>();
response.Success = true;
}
catch(Exception ex)
{
logger.ErrorException("Error processing JSON request", ex);
var response = _request.Get<AjaxResponse>();
response.Success = false;
response.Exception = ex.ToString();
}
}
public void InvokePartial()
{
_innerBehavior.InvokePartial();
}
}
But, although I get the object AjaxResponsefrom the request, any changes that I make are not returned to the client. In addition, any exceptions thrown by the action do not do this as much as possible, the request is terminated before execution falls into the catch block. What am I doing wrong?
For completeness, in my web editor, the behavior is related to the following:
Policies
.EnrichCallsWith<JsonExceptionHandlingBehaviour>(action =>
typeof(AjaxResponse).IsAssignableFrom(action.Method.ReturnType));
And the AjaxResponse looks like this:
public class AjaxResponse
{
public bool Success { get; set; }
public object Data { get; set; }
public string Exception { get; set; }
}
Jon m source
share