1<?php
2
3namespace Elastica\Exception\Bulk\Response;
4
5use Elastica\Bulk\Action;
6use Elastica\Bulk\Response as BulkResponse;
7use Elastica\Exception\BulkException;
8
9class ActionException extends BulkException
10{
11    /**
12     * @var BulkResponse
13     */
14    protected $_response;
15
16    public function __construct(BulkResponse $response)
17    {
18        $this->_response = $response;
19
20        parent::__construct($this->getErrorMessage($response));
21    }
22
23    public function getAction(): Action
24    {
25        return $this->getResponse()->getAction();
26    }
27
28    public function getResponse(): BulkResponse
29    {
30        return $this->_response;
31    }
32
33    public function getErrorMessage(BulkResponse $response): string
34    {
35        $error = $response->getError();
36        $opType = $response->getOpType();
37        $data = $response->getData();
38
39        $path = '';
40        if (isset($data['_index'])) {
41            $path .= '/'.$data['_index'];
42        }
43
44        if (isset($data['_type'])) {
45            $path .= '/'.$data['_type'];
46        }
47
48        if (isset($data['_id'])) {
49            $path .= '/'.$data['_id'];
50        }
51
52        return "{$opType}: {$path} caused {$error}";
53    }
54}
55