private static Task HandleExceptionAsync(HttpContext context, Exception ex, ILogger logger)
        {
            //   throw ex;
            var code = ex switch
            {
                NotFoundException _ => HttpStatusCode.NotFound,
                InvalidCredentialException _ => HttpStatusCode.Forbidden,
                AuthenticationException _ => HttpStatusCode.Unauthorized,
                InvalidStateException _ => HttpStatusCode.BadRequest,
                IllegalArgumentException _ => HttpStatusCode.BadRequest,
                _ => HttpStatusCode.InternalServerError
            };

            var logErrMsg = $"{ex.Message} - {context.Request.Path}{context.Request.QueryString}";

            if (code == HttpStatusCode.InternalServerError)
            {
                logger.LogError(logErrMsg);
            }
            else
            {
                logger.LogInformation(logErrMsg);
            }

            var result = JsonConvert.SerializeObject(new { error = ex.Message });

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)code;
            return(context.Response.WriteAsync(result));
        }
    }
Exemple #2
0
        /// <summary>
        ///    创建一个HBase表
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="descriptors">表描述符</param>
        /// <returns>如果创建成功,则返回可以操作该表的实例</returns>
        /// <exception cref="AlreadyExistsException">表已经存在</exception>
        /// <exception cref="IOErrorException">IO错误</exception>
        /// <exception cref="IllegalArgumentException">参数错误</exception>
        /// <exception cref="ArgumentNullException">参数不能为空</exception>
        /// <exception cref="CommunicationTimeoutException">通信超时</exception>
        /// <exception cref="CommunicationFailException">通信失败</exception>
        /// <exception cref="NoConnectionException">内部无任何可用的远程连接异常,这通常代表无法连接到任何一台远程服务器</exception>
        public IHTable CreateTable(string tableName, params ColumnDescriptor[] descriptors)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException("tableName");
            }
            if (descriptors == null || descriptors.Length == 0)
            {
                throw new ArgumentNullException("descriptors");
            }
            IThriftConnectionAgent agent = _connectionPool.GetChannel(_regionServers[0], "RegionServer", _protocolStack, _transactionManager);

            if (agent == null)
            {
                throw new NoConnectionException();
            }
            Exception ex = null;
            ThriftMessageTransaction transaction    = agent.CreateTransaction();
            AutoResetEvent           autoResetEvent = new AutoResetEvent(false);

            transaction.ResponseArrived += delegate(object sender, LightSingleArgEventArgs <ThriftMessage> e)
            {
                CreateTableResponseMessage rspMsg = (CreateTableResponseMessage)e.Target;
                if (rspMsg.IOErrorMessage != null)
                {
                    ex = new IOErrorException(rspMsg.IOErrorMessage.Reason);
                }
                else if (rspMsg.IllegalArgumentErrorMessage != null)
                {
                    ex = new IllegalArgumentException(rspMsg.IllegalArgumentErrorMessage.Reason);
                }
                else if (rspMsg.AlreadyExistsErrorMessage != null)
                {
                    ex = new AlreadyExistsException(rspMsg.AlreadyExistsErrorMessage.Reason);
                }
                autoResetEvent.Set();
            };
            transaction.Timeout += delegate
            {
                ex = new CommunicationTimeoutException(transaction.SequenceId);
                autoResetEvent.Set();
            };
            transaction.Failed += delegate
            {
                ex = new CommunicationFailException(transaction.SequenceId);
                autoResetEvent.Set();
            };
            CreateTableRequestMessage reqMsg = new CreateTableRequestMessage {
                TableName = tableName, ColumnFamilies = descriptors
            };

            transaction.SendRequest(reqMsg);
            autoResetEvent.WaitOne();
            if (ex != null)
            {
                throw ex;
            }
            return(new HTable(reqMsg.TableName, this, _hostMappingManager));
        }
Exemple #3
0
        /// <summary>
        ///		回收指定scanner资源
        /// </summary>
        /// <param name="scannerId">scannerId</param>
        /// <param name="iep">对应的Region的服务器地址</param>
        /// <exception cref="IOErrorException">IO错误</exception>
        /// <exception cref="ArgumentNullException">参数不能为空</exception>
        /// <exception cref="CommunicationTimeoutException">通信超时</exception>
        /// <exception cref="CommunicationFailException">通信失败</exception>
        internal void ScannerClose(int scannerId, IPEndPoint iep)
        {
            IThriftConnectionAgent agent = _connectionPool.GetChannel(iep, "RegionServer", _protocolStack, _transactionManager);

            if (agent == null)
            {
                throw new NoConnectionException();
            }
            Exception ex = null;
            ThriftMessageTransaction transaction    = agent.CreateTransaction();
            AutoResetEvent           autoResetEvent = new AutoResetEvent(false);

            transaction.ResponseArrived += delegate(object sender, LightSingleArgEventArgs <ThriftMessage> e)
            {
                ScannerCloseResponseMessage rspMsg = (ScannerCloseResponseMessage)e.Target;
                if (rspMsg.IOErrorMessage != null)
                {
                    ex = new IOErrorException(rspMsg.IOErrorMessage.Reason);
                }
                if (rspMsg.IOErrorMessage != null)
                {
                    ex = new IOErrorException(rspMsg.IOErrorMessage.Reason);
                }
                else if (rspMsg.IllegalArgumentErrorMessage != null)
                {
                    ex = new IllegalArgumentException(rspMsg.IllegalArgumentErrorMessage.Reason);
                }
                autoResetEvent.Set();
            };
            transaction.Timeout += delegate
            {
                ex = new CommunicationTimeoutException(transaction.SequenceId);
                autoResetEvent.Set();
            };
            transaction.Failed += delegate
            {
                ex = new CommunicationFailException(transaction.SequenceId);
                autoResetEvent.Set();
            };
            ScannerCloseRequestMessage reqMsg = new ScannerCloseRequestMessage {
                ScannerId = scannerId
            };

            transaction.SendRequest(reqMsg);
            autoResetEvent.WaitOne();
            if (ex != null)
            {
                throw ex;
            }
        }
Exemple #4
0
        ///  <summary>
        ///         插入一行数据
        ///  </summary>
        ///  <param name="tableName">表名</param>
        ///  <param name="rowkey">行键</param>
        /// <param name="iep">对应的Region的服务器地址</param>
        /// <param name="mutations">列信息</param>
        ///  <param name="attributes"></param>
        ///  <exception cref="IOErrorException">IO错误</exception>
        ///  <exception cref="IllegalArgumentException">参数错误</exception>
        ///  <exception cref="CommunicationTimeoutException">通信超时</exception>
        ///  <exception cref="CommunicationFailException">通信失败</exception>
        ///  <returns></returns>
        internal bool InsertRow(string tableName, byte[] rowkey, IPEndPoint iep, Mutation[] mutations, Dictionary <string, string> attributes = null)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException("tableName");
            }
            if (rowkey == null || rowkey.Length == 0)
            {
                throw new ArgumentNullException("rowkey");
            }
            if (mutations == null || mutations.Length == 0)
            {
                throw new ArgumentNullException("mutations");
            }
            IThriftConnectionAgent agent = _connectionPool.GetChannel(iep, "RegionServer", _protocolStack, _transactionManager);

            if (agent == null)
            {
                throw new NoConnectionException();
            }
            Exception ex = null;
            ThriftMessageTransaction transaction    = agent.CreateTransaction();
            AutoResetEvent           autoResetEvent = new AutoResetEvent(false);

            transaction.ResponseArrived += delegate(object sender, LightSingleArgEventArgs <ThriftMessage> e)
            {
                InsertNewRowResponseMessage rspMsg = (InsertNewRowResponseMessage)e.Target;
                if (rspMsg.IOErrorMessage != null)
                {
                    ex = new IOErrorException(rspMsg.IOErrorMessage.Reason);
                }
                else if (rspMsg.IllegalArgumentErrorMessage != null)
                {
                    ex = new IllegalArgumentException(rspMsg.IllegalArgumentErrorMessage.Reason);
                }
                autoResetEvent.Set();
            };
            transaction.Timeout += delegate
            {
                ex = new CommunicationTimeoutException(transaction.SequenceId);
                autoResetEvent.Set();
            };
            transaction.Failed += delegate
            {
                ex = new CommunicationFailException(transaction.SequenceId);
                autoResetEvent.Set();
            };
            InsertNewRowRequestMessage reqMsg = new InsertNewRowRequestMessage
            {
                TableName = tableName,
                RowKey    = rowkey
            };

            reqMsg.Mutations  = mutations;
            reqMsg.Attributes = attributes;
            transaction.SendRequest(reqMsg);
            autoResetEvent.WaitOne();
            if (ex != null)
            {
                throw ex;
            }
            return(true);
        }
 private void LogBadArguments(IllegalArgumentException error)
 {
     Log.Error("One or more argument(s) was invalid: " + error.getMessage());
 }
 public void OnEncodeIllegalArgumentException(IllegalArgumentException e) => HandleException(e);
 public void OnRecordIllegalArgumentException(IllegalArgumentException e) => HandleException(e);