swoft-cloud / swoft

🚀 PHP Microservice Full Coroutine Framework
https://swoft.org
Apache License 2.0
5.58k stars 788 forks source link

websocket请求cookie中存在特殊字符导致json::encode异常 #1359

Open zarte opened 4 years ago

zarte commented 4 years ago
    ERROR   php_swoole_server_rshutdown (ERRNO 503): Fatal error: Uncaught ErrorException: Uncaught InvalidArgumentException: json_encode error: Malformed UTF-8 characters, possibly incorrectly encoded in /mnt/vdc/wwwroot/dbbackweb/swoft/vendor/swoft/stdlib/src/Helper/JsonHelper.php:58
Stack trace:
#0 /mnt/vdc/wwwroot/dbbackweb/swoft/vendor/swoft/websocket-server/src/Connection.php(223): Swoft\Stdlib\Helper\JsonHelper::encode(Array)
#1 /mnt/vdc/wwwroot/dbbackweb/swoft/vendor/swoft/framework/src/Concern/AbstractSessionManager.php(83): Swoft\WebSocket\Server\Connection->toString()
#2 /mnt/vdc/wwwroot/dbbackweb/swoft/vendor/swoft/websocket-server/src/Swoole/HandshakeListener.php(81): Swoft\Concern\AbstractSessionManager->set('2152', Object(Swoft\WebSocket\Server\Connection))
#3 {main}
  thrown in /mnt/vdc/wwwroot/dbbackweb/swoft/vendor/swoft/stdlib/src/Helper/JsonHelper.php:58
Stack trace:
#0 /mnt/vdc/wwwroot/dbbackweb/swoft/vendor/swoft/error/src/DefaultErrorDispatcher.php(64): Swoft\Error\DefaultErrorDispatcher->handleError(1, 'Uncaught Invali...', '/mnt/vdc/wwwroo...', 58)
 in /mnt/vdc/wwwroot/dbbackweb/swoft/vendor/swoft/stdlib/src/Helper/JsonHelper.php on line 58

这个异常如何捕获并通过websocket返回?

inhere commented 4 years ago

swoft 多少版本的? cookies 里是怎样的数据,给个示例

zarte commented 4 years ago

@inhere 2.0.8 对应的cookie删掉了

 public static function encode($value, int $options = 0, int $depth = 512): string
    {
        $json = json_encode($value, $options, $depth);

        if (JSON_ERROR_NONE !== json_last_error()) {
            var_dump($value);
            throw new InvalidArgumentException('json_encode error: ' . json_last_error_msg());
        }

        return $json;
    }

这里打印的值