Exemplo n.º 1
0
        public async Task <JsonRpcResponse> SendRequestAsync(JsonRpcRequest rpcRequest, JsonRpcContext context)
        {
            try
            {
                (int?errorCode, string errorMessage) = Validate(rpcRequest, context);
                if (errorCode.HasValue)
                {
                    return(GetErrorResponse(rpcRequest.Method, errorCode.Value, errorMessage, null, rpcRequest.Id));
                }

                try
                {
                    return(await ExecuteRequestAsync(rpcRequest, context));
                }
                catch (TargetInvocationException ex)
                {
                    if (_logger.IsError)
                    {
                        _logger.Error($"Error during method execution, request: {rpcRequest}", ex.InnerException);
                    }
                    return(GetErrorResponse(rpcRequest.Method, ErrorCodes.InternalError, "Internal error", ex.InnerException?.ToString(), rpcRequest.Id));
                }
                catch (ModuleRentalTimeoutException ex)
                {
                    if (_logger.IsError)
                    {
                        _logger.Error($"Error during method execution, request: {rpcRequest}", ex);
                    }
                    return(GetErrorResponse(rpcRequest.Method, ErrorCodes.ModuleTimeout, "Timeout", ex.ToString(), rpcRequest.Id));
                }
                catch (Exception ex)
                {
                    if (_logger.IsError)
                    {
                        _logger.Error($"Error during method execution, request: {rpcRequest}", ex);
                    }
                    return(GetErrorResponse(rpcRequest.Method, ErrorCodes.InternalError, "Internal error", ex.ToString(), rpcRequest.Id));
                }
            }
            catch (Exception ex)
            {
                if (_logger.IsError)
                {
                    _logger.Error($"Error during validation, request: {rpcRequest}", ex);
                }
                return(GetErrorResponse(ErrorCodes.ParseError, "Parse error"));
            }
        }
Exemplo n.º 2
0
        private async Task <JsonRpcResponse> ExecuteRequestAsync(JsonRpcRequest rpcRequest, JsonRpcContext context)
        {
            string methodName = rpcRequest.Method.Trim();

            (MethodInfo MethodInfo, bool ReadOnly)result = _rpcModuleProvider.Resolve(methodName);
            return(result.MethodInfo != null
                ? await ExecuteAsync(rpcRequest, methodName, result, context)
                : GetErrorResponse(methodName, ErrorCodes.MethodNotFound, "Method not found", $"{rpcRequest.Method}", rpcRequest.Id));
        }