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
Optimized code for Hyperf\Codec\Exception\InvalidArgumentException
which you can be used to get the original data.
#6218
base: 3.0
Are you sure you want to change the base?
Conversation
InvalidJsonException
instead of InvalidArgumentException
when Json::encode
or Json::decode
failed
InvalidJsonException
instead of InvalidArgumentException
when Json::encode
or Json::decode
failedHyperf\Codec\Exception\InvalidArgumentException
which you can be used to get the origin data.
我们内部讨论了下,这个还是用户自己处理吧。 既然都抛出异常了,正常情况下肯定是预先考虑到了,那 try catch 里处理的时候,根本没必要从 exception 里拿数据,直接从上下文拿就可以了。 |
如果是在常规的业务代码中是比较好处理,若在 respone 时报错,貌似没有更好的方式,比如从数据库查出来就是乱码的,可能要排查好几个字段才能查到(多张表得到的结果) |
这样用岂不是也很简单 |
描述一个例子吧,现在确实无法说服我们大多数人合并这个 pr |
AOP 处理也可以 |
场景描述:商品详情页有 50 个相关字段,用户打开某一商品时,由于乱码导致返回 json 时 500 错误,异常信息中没有相关的数据信息,排查时只能靠人眼扫描,效率很低。如果能自定义 json 的参数也能比较友好的解决这个问题,如: |
好吧,现在这个情况,确实可以你自己处理下就能解决的 如果你使用的官方 json 方法,肯定会走到上面的方法,所以你实际上自己在外层 try catch 如果有问题,直接可以拿到 上面的 data 而非从 异常里来拿了。
当然,正常情况下,你肯定这个方法要进行封装的,类似这里 https://github.com/hyperf/biz-skeleton/blob/master/app/Kernel/Http/Response.php#L34 只需要这里处理一下就行了。 |
好的,我试试,谢大佬 |
这个 pr 不用关,先放着,是个后来可期的功能,但是暂时有点鸡肋 |
以下是 laravle 中的写法,感觉支持传参是比较好的方式 return response()->json(['status' => 1, 'msg' => substr('我是高性能、高灵活性的渐进式 PHP 协程框架',0, 4)], 200,[],JSON_INVALID_UTF8_SUBSTITUTE); |
要学会变通啊。。
你大可以封装一个方法来用啊,不一定非要用官方封装的 |
是可以,但感觉是在重复造轮子,哈哈哈 |
不重复,Hyperf 主打的就是灵活高效高性能,这符合框架的设计理念。 |
d7bf73a
to
3164682
Compare
3164682
to
1412ca1
Compare
Hyperf\Codec\Exception\InvalidArgumentException
which you can be used to get the origin data.Hyperf\Codec\Exception\InvalidArgumentException
which you can be used to get the original data.
InvalidArgumentException class
问题场景:json_encode 的内容有不完整的 utf8 字符时,会导致编码错误。异常中没有具体的数据信息,不方便排查。
添加:设置/获取原始数据的功能
使用方法:
添加以上方法后,工程师可以根据自己的情况记录日志,方便快速定位问题