You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In some situations, Elasticsearch version 8.13 returns multiple warning headers.
When there are indeed multiple warning headers, they become an array, and so the code processing them here , crashes, because it expects this.headers.warning to be a string, not an array of strings.
Example of such Error:
To Reproduce
Simple setup with the ES SDK to reproduce the behavior:
import{Client}from"@elastic/elasticsearch";asyncfunctioncrashElasticSDK(){constpayload={index: "non_existent_index",body: {query: {bool: {filter: [{exists: {field: "route",},},],},},},size: 20,_source: {include: ["*"],exclude: ["*description_embedding*"],},};constclient=newClient({node: "https://localhost:9200",maxRetries: 10,requestTimeout: 3*60*1000,});/* Below line will throw an error: TypeError: this.headers.warning.split is not a function at Object.get warnings (/Users/shaialon/github/general/sonar-ai/services/sonar-ai-schema-serving/node_modules/@elastic/transport/src/Transport.ts:363:12) at Function.entries (<anonymous>) at doRedact (/Users/shaialon/github/general/sonar-ai/services/sonar-ai-schema-serving/node_modules/@elastic/transport/src/security.ts:42:12) at redactObject (/Users/shaialon/github/general/sonar-ai/services/sonar-ai-schema-serving/node_modules/@elastic/transport/src/security.ts:38:10) at redactDiagnostic (/Users/shaialon/github/general/sonar-ai/services/sonar-ai-schema-serving/node_modules/@elastic/transport/src/security.ts:89:14) at ResponseError (/Users/shaialon/github/general/sonar-ai/services/sonar-ai-schema-serving/node_modules/@elastic/transport/src/errors.ts:154:33) at SniffingTransport.request (/Users/shaialon/github/general/sonar-ai/services/sonar-ai-schema-serving/node_modules/@elastic/transport/src/Transport.ts:553:17) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at Client.SearchApi (/Users/shaialon/github/general/sonar-ai/services/sonar-ai-schema-serving/node_modules/@elastic/elasticsearch/src/api/api/search.ts:89:10) at crashElasticSDK (/Users/shaialon/github/general/sonar-ai/services/sonar-ai-schema-serving/src/services/elasticsearch_client.ts:33:17) */try{constvalue=awaitclient.search(payload);// This line will never be reachedconsole.log("this line will never be reached");returnvalue;}catch(err){console.error(err);}}crashElasticSDK();
The code above creates the warnings from the elasticsearch headers:
Warning: 299 Elasticsearch-8.13.2-16cc90cd2d08a3147ce02b07e50894bc060a4cbf "Deprecated field [include] used, expected [includes] instead"
Warning: 299 Elasticsearch-8.13.2-16cc90cd2d08a3147ce02b07e50894bc060a4cbf "Deprecated field [exclude] used, expected [excludes] instead"
You can see the exact ES response that crashes the transport library with this request to a cluster:
getwarnings(){// Check if warning header is absentif(this.headers?.warning==null){returnnull;}// Ensure handling both single string and array of stringsconstwarnings=Array.isArray(this.headers.warning) ? this.headers.warning : [this.headers.warning];// Split only if necessary, then flatten and filterreturnwarnings.flatMap(warning=>warning.split(/(?!\B"[^"]*),(?![^"]*"\B)/))// Split each warning entry on commas that aren't enclosed in quotes.filter(warning=>warning.match(/^\d\d\dElasticsearch-/));// Filter messages with specific format}
Updates the get warnings() function to robustly handle both scenarios: when this.headers.warning is a string and when it is an array of strings. The key changes are:
Unified Handling of String and Array: The function first checks if this.headers.warning is an array. If not, it wraps the string into an array. This ensures that the subsequent operations, which are designed for arrays, can proceed uniformly regardless of the input type.
Safe Splitting and Filtering: Using .flatMap(), the function splits each warning entry on commas not enclosed in quotes and flattens the results. It then filters these entries to include only those that match the expected Elasticsearch warning format (three digits followed by the keyword Elasticsearch-).
The text was updated successfully, but these errors were encountered:
🐛 Bug Report
In some situations, Elasticsearch version 8.13 returns multiple
warning
headers.When there are indeed multiple warning headers, they become an array, and so the code processing them here , crashes, because it expects
this.headers.warning
to be a string, not an array of strings.Example of such Error:
To Reproduce
Simple setup with the ES SDK to reproduce the behavior:
The code above creates the warnings from the elasticsearch headers:
You can see the exact ES response that crashes the transport library with this request to a cluster:
Which returns:
Expected behavior
This line should expect an array in some cases as
this.headers.warning
, in which case different logic is needed so the code does not crash.The code below should be resilient to getting an array under
this.headers.warning
Your Environment
@elastic/elasticsearch
version: >=8.13.1Suggested solution:
Updates the
get warnings()
function to robustly handle both scenarios: whenthis.headers.warning
is a string and when it is an array of strings. The key changes are:Unified Handling of String and Array: The function first checks if this.headers.warning is an array. If not, it wraps the string into an array. This ensures that the subsequent operations, which are designed for arrays, can proceed uniformly regardless of the input type.
Safe Splitting and Filtering: Using
.flatMap()
, the function splits each warning entry on commas not enclosed in quotes and flattens the results. It then filters these entries to include only those that match the expected Elasticsearch warning format (three digits followed by the keyword Elasticsearch-).The text was updated successfully, but these errors were encountered: