zeromicro / go-zero

A cloud-native Go microservices framework with cli tool for productivity.
https://go-zero.dev
MIT License
29.32k stars 3.96k forks source link

rest default middleware (recoverhandler) can input self function? #4273

Open Owen-Zhang opened 3 months ago

Owen-Zhang commented 3 months ago
  1. rest 中, RecoverHandler 能否搞一个入参function, 很多时候出现panic之后,想给用户提示一些比较好的信息, 但现在框架是没有的,框架这里也没法统一提示信息, 因此最好提供一种回调方式,让用户自己手动处理

  2. rest chain中,我个人感觉应该将RecoverHandler提前,记日志, recover,后面再是其它 middleware

  3. 还有就是,我个人感觉,middleware是否可以搞成: middleware中间不要记录日志,如果有错误(可以构造一个通用的结构体)就是直接返回到上级(如果最上级是记录日志的话),那个地方统一记录日志,同时对错误信息进行特殊处理, 将相关信息返回给前端, 但有一个问题就是日志middleware可能不会打开, 上面的处理就有问题,但可以在日志middleware中加上开关,是否记录日志等, 现实中正常情况下可能不会记录,但出错一定记录日志的。

Issues-translate-bot commented 3 months ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


  1. In rest, can RecoverHandler have a parameter input function? Many times after a panic occurs, I want to prompt the user with some better information, but now the framework does not have it, and the framework cannot uniformly prompt information, so it is best to provide A callback method that allows users to handle it manually
  1. In the rest chain, I personally feel that RecoverHandler should be implemented in advance to record logs and recover, followed by other middleware.
princejha95 commented 2 months ago

@Owen-Zhang If i got the requirement correctly, RecoverHandler should also accept an optional argument of type function which will be called whenever panic occurs. If the argument is not passed, existing flow will be continued.

Is my understanding correct or am i missing something ?

kevwan commented 2 months ago

You can define your middleware to handle panics. I think it meets your requirements.

Use svr.Use(..).