public override IRuleResult Invoke() { var ruleResult = new RuleResult(); if (UnhandledException?.GetType() == typeof(Exception)) { ruleResult.Error = new Error { Exception = UnhandledException }; } return(ruleResult); }
public override Task <IRuleResult> InvokeAsync() { var ruleResult = new RuleResult(); if (UnhandledException?.GetType() == typeof(Exception)) { ruleResult.Error = new Error { Exception = UnhandledException }; } return(RuleResult.CreateAsync(ruleResult)); }
public async Task <ResponseBase> ExecuteCommand <T>(CommandBase command, IServiceProvider serviceProvider, HttpInformation information, ILogger <T> logger, ConnectionBase connection = null) { ExecutionContext executionContext = new ExecutionContext(); string commandTypeName = command.GetType().Name; Type handlerType = commandHandlerTypes.GetValueOrDefault(commandTypeName); try { if (handlerType == null) { throw new CommandHandlerNotFoundException(commandTypeName); } object handler = serviceProvider.GetService(handlerType); if (handler == null) { throw new CommandHandlerNotFoundException(commandTypeName); } logger.LogInformation("Handling {command} with {handler}. ConnectionId: {connectionId}, ExecutionId: {executionId}", command.GetType().Name, handler.GetType().Name, connection?.Id, executionContext.Id); if (!options.CanExecuteCommand(command, information)) { throw new UnauthorizedException("You are not allowed to execute this command"); } if (handler is INeedsConnection handlerWithConnection) { if (handler is ExecuteCommandHandler || connection != null) { handlerWithConnection.Connection = connection; } else { throw new MissingRealtimeConnectionException(command.GetType().Name); } } ResponseBase response = await(dynamic) handlerType.GetHandlerHandleMethod() .Invoke(handler, new object[] { information, command, executionContext }); logger.LogInformation("Handled {command}. ConnectionId: {connectionId}, ExecutionId: {executionId}", command.GetType().Name, connection?.Id, executionContext.Id); return(response); } catch (Exception ex) { if (ex is TargetInvocationException targetInvocationException && targetInvocationException.InnerException != null) { ex = ex.InnerException; } if (!(ex is SapphireDbException sapphireDbException)) { sapphireDbException = new UnhandledException(ex); } if (sapphireDbException.Severity == ExceptionSeverity.Warning) { logger.LogWarning(sapphireDbException, "The command handler returned an error with low severity during handling of {command}. Error: {error}, ErrorId: {errorId}, ConnectionId: {connectionId}, ExecutionId: {executionId}", command.GetType().Name, sapphireDbException.GetType().Name, sapphireDbException.Id, connection?.Id, executionContext.Id); } else { logger.LogError(sapphireDbException, "Error handling {command}. Error: {error}, ErrorId: {errorId}, ConnectionId: {connectionId}, ExecutionId: {executionId}", command.GetType().Name, sapphireDbException.GetType().Name, sapphireDbException.Id, connection?.Id, executionContext.Id); } return(new ResponseBase() { ReferenceId = command.ReferenceId, Error = new SapphireDbErrorResponse(sapphireDbException) }); } }