mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-20 04:24:13 +00:00
perf[net]: 添加异常处理返回消息
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.net.packet.common;
|
||||
|
||||
import com.baidu.bjf.remoting.protobuf.annotation.Ignore;
|
||||
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
|
||||
import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
/**
|
||||
* @author meiw
|
||||
* @version 3.0
|
||||
*/
|
||||
@ProtobufClass
|
||||
public class ErrorCode implements IPacket {
|
||||
|
||||
@Ignore
|
||||
public static final transient short PROTOCOL_ID = 105;
|
||||
|
||||
@Protobuf(order = 1)
|
||||
private int protocolId;
|
||||
@Protobuf(order = 2)
|
||||
private int errorCode;
|
||||
@Protobuf(order = 3)
|
||||
private String errorMessage;
|
||||
|
||||
@Override
|
||||
public short protocolId() {
|
||||
return PROTOCOL_ID;
|
||||
}
|
||||
|
||||
public static ErrorCode valueOf(int protocolId, int errorCode, String errorMessage) {
|
||||
ErrorCode response = new ErrorCode();
|
||||
response.protocolId = protocolId;
|
||||
response.errorCode = errorCode;
|
||||
response.errorMessage = errorMessage;
|
||||
return response;
|
||||
}
|
||||
|
||||
public static ErrorCode valueOf(IPacket packet, int errorCode, String errorMessage) {
|
||||
ErrorCode response = new ErrorCode();
|
||||
response.protocolId = packet.protocolId();
|
||||
response.errorCode = errorCode;
|
||||
response.errorMessage = errorMessage;
|
||||
return response;
|
||||
}
|
||||
|
||||
public static ErrorCode valueOf(IPacket packet, int errorCode) {
|
||||
return valueOf(packet, errorCode, "");
|
||||
}
|
||||
|
||||
public int getProtocolId() {
|
||||
return protocolId;
|
||||
}
|
||||
|
||||
public void setProtocolId(int protocolId) {
|
||||
this.protocolId = protocolId;
|
||||
}
|
||||
|
||||
public int getErrorCode() {
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
public void setErrorCode(int errorCode) {
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
public String getErrorMessage() {
|
||||
return errorMessage;
|
||||
}
|
||||
|
||||
public void setErrorMessage(String errorMessage) {
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import com.zfoo.net.core.gateway.model.AuthUidToGatewayCheck;
|
||||
import com.zfoo.net.core.gateway.model.AuthUidToGatewayConfirm;
|
||||
import com.zfoo.net.core.gateway.model.AuthUidToGatewayEvent;
|
||||
import com.zfoo.net.packet.common.Error;
|
||||
import com.zfoo.net.packet.common.ErrorCode;
|
||||
import com.zfoo.net.packet.common.Heartbeat;
|
||||
import com.zfoo.net.packet.model.EncodedPacketInfo;
|
||||
import com.zfoo.net.router.answer.AsyncAnswer;
|
||||
@@ -26,6 +27,7 @@ import com.zfoo.net.router.answer.SyncAnswer;
|
||||
import com.zfoo.net.router.attachment.GatewayAttachment;
|
||||
import com.zfoo.net.router.attachment.IAttachment;
|
||||
import com.zfoo.net.router.attachment.SignalAttachment;
|
||||
import com.zfoo.net.router.exception.ErrorRequestException;
|
||||
import com.zfoo.net.router.exception.ErrorResponseException;
|
||||
import com.zfoo.net.router.exception.NetTimeOutException;
|
||||
import com.zfoo.net.router.exception.UnexpectedProtocolException;
|
||||
@@ -342,6 +344,7 @@ public class Router implements IRouter {
|
||||
// 这个在哪个线程处理取决于:这个上层的PacketReceiverTask被丢到了哪个线程中
|
||||
PacketBus.submit(session, packet, attachment);
|
||||
} catch (Exception e) {
|
||||
handleException(session, packet, attachment, e);
|
||||
logger.error(StringUtils.format("e[uid:{}][sid:{}]未知exception异常", session.getAttribute(AttributeType.UID), session.getSid(), e.getMessage()), e);
|
||||
} catch (Throwable t) {
|
||||
logger.error(StringUtils.format("e[uid:{}][sid:{}]未知error错误", session.getAttribute(AttributeType.UID), session.getSid(), t.getMessage()), t);
|
||||
@@ -359,4 +362,17 @@ public class Router implements IRouter {
|
||||
}
|
||||
}
|
||||
|
||||
private void handleException(Session session, IPacket packet, IAttachment attachment, Exception exception) {
|
||||
if (exception instanceof ErrorRequestException) {
|
||||
ErrorRequestException requestException = (ErrorRequestException) exception;
|
||||
int errorCode = requestException.getErrorCode();
|
||||
String msg = requestException.getErrorMsg();
|
||||
var resp = ErrorCode.valueOf(packet, errorCode, msg);
|
||||
NetContext.getRouter().send(session, resp, attachment);
|
||||
return;
|
||||
}
|
||||
var resp = ErrorCode.valueOf(packet, 0);
|
||||
NetContext.getRouter().send(session, resp, attachment);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.net.router.exception;
|
||||
|
||||
/**
|
||||
* @author meiw
|
||||
* @version 3.0
|
||||
*/
|
||||
public class ErrorRequestException extends RuntimeException {
|
||||
|
||||
private final int errorCode;
|
||||
|
||||
private final String errorMsg;
|
||||
|
||||
public ErrorRequestException(int errorCode, String errorMsg) {
|
||||
super(errorMsg);
|
||||
this.errorCode = errorCode;
|
||||
this.errorMsg = errorMsg;
|
||||
}
|
||||
|
||||
public ErrorRequestException(int errorCode) {
|
||||
this.errorCode = errorCode;
|
||||
this.errorMsg = "";
|
||||
}
|
||||
|
||||
public int getErrorCode() {
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
public String getErrorMsg() {
|
||||
return errorMsg;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user