private void DistributeExecution(object payload, object context, Envelope envelope, ICommand commandWithKey, HandlerDescriptor handler) { object key = distributor.Distribute(payload); queue.Enqueue(key, async() => { Action <Exception> additionalExceptionDecorator = e => { AggregateRootException aggregateException = e as AggregateRootException; if (aggregateException != null) { // If envelope is created and contains source command key, use it. IKey sourceCommandKey; if (aggregateException.SourceCommandKey == null && envelope != null && envelope.TryGetSourceCommandKey(out sourceCommandKey)) { aggregateException.SourceCommandKey = sourceCommandKey; } // If command is command with key, use it. if (aggregateException.CommandKey == null && commandWithKey != null) { aggregateException.CommandKey = commandWithKey.Key; } } }; try { if (handler.IsContext) { await handler.Execute(context, additionalExceptionDecorator); } else if (handler.IsEnvelope) { await handler.Execute(envelope, additionalExceptionDecorator); } else if (handler.IsPlain) { await handler.Execute(payload, additionalExceptionDecorator); } else { throw Ensure.Exception.UndefinedHandlerType(handler); } // If we have command with the key, notify about successful execution. if (store != null && commandWithKey != null) { await store.PublishedAsync(commandWithKey.Key); } } catch (Exception e) { DispatcherExceptionHandlers.Handle(e); } }); }
private ActionResult ProcessException(AggregateRootException e) { string exceptionType = e.GetType().AssemblyQualifiedName; string exceptionPayload = formatters.Exception.Serialize(e); var response = new Response() { Type = exceptionType, Payload = exceptionPayload }; return(new ContentResult() { Content = json.Serialize(response), StatusCode = 500, ContentType = "text/json" }); }
/// <summary> /// Creates a new instance for modifying <paramref name="exception"/>. /// </summary> /// <param name="exception">An instance of aggregate root exception.</param> public AggregateRootExceptionExtender(AggregateRootException exception) { Ensure.NotNull(exception, "exception"); Exception = exception; }