silverstripe / silverstripe-search-service

A service-agnostic search module for Silverstripe CMS
BSD 3-Clause "New" or "Revised" License
5 stars 18 forks source link

BUGFIX ensure truncated strings are still json_encodeable #60

Closed andrewandante closed 3 years ago

andrewandante commented 3 years ago

Fixes an error I was seeing a lot:

[ERROR] Invalid request body provided {"exception":"[object] (InvalidArgumentException(code: 0): Invalid request body provided at /var/www/mysite/www/vendor/guzzlehttp/ringphp/src/Client/CurlFactory.php:289)"} []

It was coming from truncating content that was in Thai (amongst other languages) that would have characters cut through the middle, making them invalid UTF-8 and causing the json_encode() to fail when Guzzle was trying to compile the body to fire through to Elastic.

This PR checks that the string is json_encode()-able and nudges the truncate one char each loop until it is.

michalkleiner commented 3 years ago

Would using multi-byte compatible functions (mb_substr and mb_strlen) do the job without potentially trying to encode the data several times in the loop?

andrewandante commented 3 years ago

Would using multi-byte compatible functions (mb_substr and mb_strlen) do the job without potentially trying to encode the data several times in the loop?

Hmm fair point, that seems to work as well - will try on a full reindex and confirm

andrewandante commented 3 years ago

Yep much cleaner and has the same effect - nice spot @michalkleiner