Cet article est aussi disponible en français
A strange issue I ran into on a Drupal project with searchApi connected to SearchStax.
On some pages I was getting a 500 "internal server error" response, while many others worked without any problem... There aren't really any logs on SearchStax to dig into, nor any debug option. Strangest of all, the same request in Postman got a correct response, but sending it with the curl binary failed too.
It seemed to happen on very large requests: on one of the problematic ones, the request body was close to 8 KB, yes, more than 8,000 characters.
When I finally compared the headers, the solution turned out to be the "Accept-Encoding: gzip" header that Postman sends by default. Adding it to the curl request made it work.
So I used an event to add it to all searchApi requests:
mon_module/src/EventSubscriber/MonModuleSearchSubscriber.php
<?php
namespace Drupal\mon_module\EventSubscriber;
use Solarium\Core\Event\PostCreateRequest as PostCreateRequestEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Pop_produit event subscriber.
*/
class MonModuleSearchSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents(): array {
return [
PostCreateRequestEvent::class => ['postCreateRequestAddHeader'],
];
}
public function postCreateRequestAddHeader(PostCreateRequestEvent $e) {
$request = $e->getRequest();
$request->addHeader('Accept-Encoding: gzip');
}
}
No more errors, but now the response comes back gzip-encoded and needs to be decoded. An event can't be used here, so I had to write a patch for the search_api_solr/Plugin/search_api/backend/SearchApiSolrBackend.php file:
diff --git a/src/Plugin/search_api/backend/SearchApiSolrBackend.php b/src/Plugin/search_api/backend/SearchApiSolrBackend.php
index 752df404..0af49932 100644
--- a/src/Plugin/search_api/backend/SearchApiSolrBackend.php
+++ b/src/Plugin/search_api/backend/SearchApiSolrBackend.php
@@ -1930,6 +1930,9 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter
// Send search request.
$response = $connector->search($solarium_query, $this->getCollectionEndpoint($index));
$body = $response->getBody();
+ if (@gzdecode($body) !== FALSE) {
+ $body = gzdecode($body);
+ }
if (200 != $response->getStatusCode()) {
throw new SearchApiSolrException(strip_tags($body), $response->getStatusCode());
}and add it to my composer.json (see below to learn more about patches and Composer):
"patches": {
"drupal/search_api_solr": {
"Gzip Encoding": "./files/patches/search_api_solr_gzip_encoding.patch"
}
}It's not great, and there may be a cleaner solution, but I hope this helps someone, since resources on the subject seem to be nonexistent. The SearchStax page about this error code gives no hint pointing to this kind of solution.
Thanks to Romain for putting me on the right track!
Comments (0)