Skip to content

QueryException crashes with "Undefined array key 'error'" when processing malformed OpenSearch responses #21

@JustinShift

Description

@JustinShift

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:

  1. Perform bulk indexing operations that may encounter OpenSearch errors
  2. Have OpenSearch return an error response that doesn't contain the error.type structure
  3. The QueryException constructor will fail when trying to format the error message
  4. 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:

  1. Return the original error message when the expected structure is not present
  2. Have defensive checks for required keys before accessing them
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions