Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActionAfter 异常信息携带到请求端 #26

Closed
iohao opened this issue Dec 3, 2022 · 2 comments
Closed

ActionAfter 异常信息携带到请求端 #26

iohao opened this issue Dec 3, 2022 · 2 comments
Assignees
Labels
Support extension;可扩展 Support extension;可扩展 trick;使用技巧 trick;使用技巧

Comments

@iohao
Copy link
Owner

iohao commented Dec 3, 2022

see https://gitee.com/iohao/iogame/issues/I5KZTP

业务框架的异常机制,只保存错误码,不会保存错误码对应的异常信息,这是因为:

从字段精简的角度,我们不可能每次响应都带上完整的异常信息给客户端排查问题,因此,我们会定义一些响应码,通过编号进行网络传输,方便客户端定位问题。

但有一种情况是会携带异常信息,就是开启 JSR380 时,具体参考 业务框架开启JSR验证


配合游戏文档生成
https://www.yuque.com/iohao/game/irth38

==================== TankAction 坦克相关 ====================
路由: 2 - 2  --- 【玩家进入房间】 --- 【TankAction:135】【enterRoom】
    方法参数: com.iohao.game.collect.proto.tank.TankEnterRoom
    方法返回值: com.iohao.game.collect.proto.tank.TankEnterRoom
 
 
路由: 2 - 5  --- 【坦克移动】 --- 【TankAction:101】【tankMove】
    方法参数: com.iohao.game.collect.proto.tank.TankLocation
    广播推送: com.iohao.game.collect.proto.tank.TankLocation
 
路由: 2 - 6  --- 【坦克射击(发射子弹)】 --- 【TankAction:74】【shooting】
    触发异常: (方法有可能会触发异常)
    方法参数: com.iohao.game.collect.proto.tank.TankBullet
    广播推送: com.iohao.game.collect.proto.tank.TankBullet
 
==================== 其它广播推送 ====================
路由: 2 - 11  --- 广播推送: com.iohao.game.collect.proto.common.UserInfo

==================== 错误码 ====================
 -1002 : 路由错误码,一般是客户端请求了不存在的路由引起的 
 -1001 : 参数验错误码 
 -1000 : 系统其它错误 
 -1 : class 不存在 
 201 : 子弹不存在或不足 

在生成的游戏文档中,可以看见所有的错误码;
如果做国际化,放在前端会比较好做一些。

@iohao
Copy link
Owner Author

iohao commented Dec 3, 2022

默认情况下,框架是不会携带异常信息的,原因上面已经提过了。

但我们可以通过扩展的方式来实现,将异常信息携带到请求端,如下:

框架在 ActionMethodResultWrap 时,已经把异常信息存放到 flowContext.option(FlowAttr.msgException, msgException.getMessage()) 中了。

之后我们重写一个 ActionAfter 类,并配置到业务框架中,把默认的实现 DefaultActionAfter 替换就可以了。

重写的 ActionAfter 实现类中,将这个异常信息在发送之前保存到 response 中就可以。

public final class MyActionAfter implements ActionAfter {
    ... ... 省略部分代码

    @Override
    public void execute(final FlowContext flowContext) {
        ... ... 省略部分代码

        // 有错误就响应给调用方
        if (response.hasError()) {
            // 异常消息发送到请求端
            String msg = flowContext.option(FlowAttr.msgException);
            response.setValidatorMsg(msg);

            asyncCtx.sendResponse(response);
            return;
        }

        ... ... 省略部分代码
    }
}

@iohao iohao added the trick;使用技巧 trick;使用技巧 label Dec 3, 2022
@iohao iohao closed this as completed Dec 3, 2022
@iohao iohao self-assigned this Feb 1, 2023
@iohao iohao added the Support extension;可扩展 Support extension;可扩展 label Mar 17, 2023
@iohao
Copy link
Owner Author

iohao commented Mar 29, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Support extension;可扩展 Support extension;可扩展 trick;使用技巧 trick;使用技巧
Projects
None yet
Development

No branches or pull requests

1 participant