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