private static async Task ReportExceptionToClient(SubscriptionConnection connection, Exception ex, int recursionDepth = 0) { if (recursionDepth == 2) { return; } try { if (ex is SubscriptionDoesNotExistException) { await connection.WriteJsonAsync(new DynamicJsonValue { [nameof(SubscriptionConnectionServerMessage.Type)] = nameof(SubscriptionConnectionServerMessage.MessageType.ConnectionStatus), [nameof(SubscriptionConnectionServerMessage.Status)] = nameof(SubscriptionConnectionServerMessage.ConnectionStatus.NotFound), [nameof(SubscriptionConnectionServerMessage.Message)] = ex.Message, [nameof(SubscriptionConnectionServerMessage.Exception)] = ex.ToString() }); } else if (ex is SubscriptionClosedException) { await connection.WriteJsonAsync(new DynamicJsonValue { [nameof(SubscriptionConnectionServerMessage.Type)] = nameof(SubscriptionConnectionServerMessage.MessageType.ConnectionStatus), [nameof(SubscriptionConnectionServerMessage.Status)] = nameof(SubscriptionConnectionServerMessage.ConnectionStatus.Closed), [nameof(SubscriptionConnectionServerMessage.Message)] = ex.Message, [nameof(SubscriptionConnectionServerMessage.Exception)] = ex.ToString() }); } else if (ex is SubscriptionInvalidStateException) { await connection.WriteJsonAsync(new DynamicJsonValue { [nameof(SubscriptionConnectionServerMessage.Type)] = nameof(SubscriptionConnectionServerMessage.MessageType.ConnectionStatus), [nameof(SubscriptionConnectionServerMessage.Status)] = nameof(SubscriptionConnectionServerMessage.ConnectionStatus.Invalid), [nameof(SubscriptionConnectionServerMessage.Message)] = ex.Message, [nameof(SubscriptionConnectionServerMessage.Exception)] = ex.ToString() }); } else if (ex is SubscriptionInUseException) { await connection.WriteJsonAsync(new DynamicJsonValue { [nameof(SubscriptionConnectionServerMessage.Type)] = nameof(SubscriptionConnectionServerMessage.MessageType.ConnectionStatus), [nameof(SubscriptionConnectionServerMessage.Status)] = nameof(SubscriptionConnectionServerMessage.ConnectionStatus.InUse), [nameof(SubscriptionConnectionServerMessage.Message)] = ex.Message, [nameof(SubscriptionConnectionServerMessage.Exception)] = ex.ToString() }); } else if (ex is SubscriptionDoesNotBelongToNodeException subscriptionDoesNotBelongException) { if (connection._logger.IsInfoEnabled) { connection._logger.Info("Subscription does not belong to current node", ex); } await connection.WriteJsonAsync(new DynamicJsonValue { [nameof(SubscriptionConnectionServerMessage.Type)] = nameof(SubscriptionConnectionServerMessage.MessageType.ConnectionStatus), [nameof(SubscriptionConnectionServerMessage.Status)] = nameof(SubscriptionConnectionServerMessage.ConnectionStatus.Redirect), [nameof(SubscriptionConnectionServerMessage.Message)] = ex.Message, [nameof(SubscriptionConnectionServerMessage.Data)] = new DynamicJsonValue { [nameof(SubscriptionConnectionServerMessage.SubscriptionRedirectData.RedirectedTag)] = subscriptionDoesNotBelongException.AppropriateNode } }); } else if (ex is SubscriptionChangeVectorUpdateConcurrencyException subscriptionConcurrency) { if (connection._logger.IsInfoEnabled) { connection._logger.Info("Subscription change vector update concurrency error", ex); } await connection.WriteJsonAsync(new DynamicJsonValue { [nameof(SubscriptionConnectionServerMessage.Type)] = nameof(SubscriptionConnectionServerMessage.MessageType.ConnectionStatus), [nameof(SubscriptionConnectionServerMessage.Status)] = nameof(SubscriptionConnectionServerMessage.ConnectionStatus.ConcurrencyReconnect), [nameof(SubscriptionConnectionServerMessage.Message)] = ex.Message, [nameof(SubscriptionConnectionServerMessage.Exception)] = ex.ToString() }); } else if (ex is CommandExecutionException commandExecution && commandExecution.InnerException is SubscriptionException) { await ReportExceptionToClient(connection, commandExecution.InnerException, recursionDepth - 1); } else { if (connection._logger.IsInfoEnabled) { connection._logger.Info("Subscription error", ex); } await connection.WriteJsonAsync(new DynamicJsonValue { [nameof(SubscriptionConnectionServerMessage.Type)] = nameof(SubscriptionConnectionServerMessage.MessageType.Error), [nameof(SubscriptionConnectionServerMessage.Status)] = nameof(SubscriptionConnectionServerMessage.ConnectionStatus.None), [nameof(SubscriptionConnectionServerMessage.Message)] = ex.Message, [nameof(SubscriptionConnectionServerMessage.Exception)] = ex.ToString() }); } }
private static async Task ReportExceptionToClient(SubscriptionConnection connection, Exception ex) { try { if (ex is SubscriptionDoesNotExistException) { await connection.WriteJsonAsync(new DynamicJsonValue { [nameof(SubscriptionConnectionServerMessage.Type)] = nameof(SubscriptionConnectionServerMessage.MessageType.ConnectionStatus), [nameof(SubscriptionConnectionServerMessage.Status)] = nameof(SubscriptionConnectionServerMessage.ConnectionStatus.NotFound), [nameof(SubscriptionConnectionServerMessage.Message)] = ex.Message, [nameof(SubscriptionConnectionServerMessage.Exception)] = ex.ToString() }); } else if (ex is SubscriptionClosedException) { await connection.WriteJsonAsync(new DynamicJsonValue { [nameof(SubscriptionConnectionServerMessage.Type)] = nameof(SubscriptionConnectionServerMessage.MessageType.ConnectionStatus), [nameof(SubscriptionConnectionServerMessage.Status)] = nameof(SubscriptionConnectionServerMessage.ConnectionStatus.Closed), [nameof(SubscriptionConnectionServerMessage.Message)] = ex.Message, [nameof(SubscriptionConnectionServerMessage.Exception)] = ex.ToString() }); } else if (ex is SubscriptionInvalidStateException) { await connection.WriteJsonAsync(new DynamicJsonValue { [nameof(SubscriptionConnectionServerMessage.Type)] = nameof(SubscriptionConnectionServerMessage.MessageType.ConnectionStatus), [nameof(SubscriptionConnectionServerMessage.Status)] = nameof(SubscriptionConnectionServerMessage.ConnectionStatus.Invalid), [nameof(SubscriptionConnectionServerMessage.Message)] = ex.Message, [nameof(SubscriptionConnectionServerMessage.Exception)] = ex.ToString() }); } else if (ex is SubscriptionInUseException) { await connection.WriteJsonAsync(new DynamicJsonValue { [nameof(SubscriptionConnectionServerMessage.Type)] = nameof(SubscriptionConnectionServerMessage.MessageType.ConnectionStatus), [nameof(SubscriptionConnectionServerMessage.Status)] = nameof(SubscriptionConnectionServerMessage.ConnectionStatus.InUse), [nameof(SubscriptionConnectionServerMessage.Message)] = ex.Message, [nameof(SubscriptionConnectionServerMessage.Exception)] = ex.ToString() }); } else if (ex is SubscriptionDoesNotBelongToNodeException subscriptionDoesNotBelongException) { if (connection._logger.IsInfoEnabled) { connection._logger.Info("Subscription does not belong to current node", ex); } await connection.WriteJsonAsync(new DynamicJsonValue { [nameof(SubscriptionConnectionServerMessage.Type)] = nameof(SubscriptionConnectionServerMessage.MessageType.ConnectionStatus), [nameof(SubscriptionConnectionServerMessage.Status)] = nameof(SubscriptionConnectionServerMessage.ConnectionStatus.Redirect), [nameof(SubscriptionConnectionServerMessage.Message)] = ex.Message, [nameof(SubscriptionConnectionServerMessage.Data)] = new DynamicJsonValue { [nameof(SubscriptionConnectionServerMessage.SubscriptionRedirectData.RedirectedTag)] = subscriptionDoesNotBelongException.AppropriateNode } }); } else { await connection.WriteJsonAsync(new DynamicJsonValue { [nameof(SubscriptionConnectionServerMessage.Type)] = nameof(SubscriptionConnectionServerMessage.MessageType.Error), [nameof(SubscriptionConnectionServerMessage.Status)] = nameof(SubscriptionConnectionServerMessage.ConnectionStatus.None), [nameof(SubscriptionConnectionServerMessage.Message)] = ex.Message, [nameof(SubscriptionConnectionServerMessage.Exception)] = ex.ToString() }); } } catch { // ignored } }