-
Notifications
You must be signed in to change notification settings - Fork 5
Description
Package version
v3.0.2
Describe the bug
The QueryException class crashes with a PHP ErrorException: "Undefined array key 'error'" when trying to format error messages from OpenSearch responses that don't contain the expected error key structure.
This prevents proper error handling and causes indexing operations to crash instead of gracefully handling OpenSearch errors.
The issue occurs in src/Exceptions/QueryException.php at line 61 where the code assumes all error responses have an error.type structure without checking if the error key exists:
return match ($error['error']['type']) {
'search_phase_execution_exception' => $this->formatSearchPhaseExecutionException($error),
'script_exception' => $this->formatScriptException($error),
default => $this->formatParseException($error),
};To Reproduce
Steps to reproduce the behavior:
- Perform bulk indexing operations that may encounter OpenSearch errors
- Have OpenSearch return an error response that doesn't contain the
error.typestructure - The
QueryExceptionconstructor will fail when trying to format the error message - Application crashes with
ErrorException: "Undefined array key 'error'"
Expected behavior
The QueryException should handle malformed or unexpected OpenSearch error response structures gracefully without crashing. It should either:
- Return the original error message when the expected structure is not present
- Have defensive checks for required keys before accessing them
- Provide a fallback error message format for unexpected response structures
Error Logs
Stack trace from logs:
"error":"Undefined array key \"error\"",
"exception_class":"ErrorException",
"trace":"#0 HandleExceptions.php(255): HandleExceptions->handleError()
#1 QueryException.php(61): HandleExceptions->Illuminate\\Foundation\\Bootstrap\\{closure}()
#2 QueryException.php(43): QueryException->routeClientResponseException()
#3 QueryException.php(31): QueryException->formatMessage()
#4 Connection.php(580): QueryException->__construct()"
Suggestion
A simple fix would be to add a defensive check:
// Add this check before line 61
if (!isset($error['error']['type'])) {
return $result->getMessage(); // Return original message if structure is unexpected
}or if applicable.
// Add this check before line 61
if (!isset($error['error']['type'])) {
return $this->formatParseException($error) // Return default exception handling
}This would allow the QueryException to be properly constructed and contain the original OpenSearch error information, rather than crashing the entire operation.