1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * Copyright OpenSearch Contributors
7: * SPDX-License-Identifier: Apache-2.0
8: *
9: * OpenSearch PHP client
10: *
11: * @link https://github.com/opensearch-project/opensearch-php/
12: * @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
13: * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
14: * @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
15: *
16: * Licensed to Elasticsearch B.V under one or more agreements.
17: * Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
18: * the GNU Lesser General Public License, Version 2.1, at your option.
19: * See the LICENSE file in the project root for more information.
20: */
21:
22: namespace OpenSearch\Common\Exceptions\Serializer;
23:
24: use OpenSearch\Common\Exceptions\OpenSearchException;
25:
26: /**
27: * Class JsonErrorException
28: */
29: class JsonErrorException extends \Exception implements OpenSearchException
30: {
31: /**
32: * @var mixed
33: */
34: private $input;
35:
36: /**
37: * @var mixed
38: */
39: private $result;
40:
41: /**
42: * @var string[]
43: */
44: private static $messages = array(
45: JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
46: JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
47: JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
48: JSON_ERROR_SYNTAX => 'Syntax error',
49: JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
50: JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded',
51: JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded',
52: JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given',
53:
54: // JSON_ERROR_* constant values that are available on PHP >= 7.0
55: 9 => 'Decoding of value would result in invalid PHP property name', //JSON_ERROR_INVALID_PROPERTY_NAME
56: 10 => 'Attempted to decode nonexistent UTF-16 code-point' //JSON_ERROR_UTF16
57: );
58:
59: /**
60: * @param mixed $input
61: * @param mixed $result
62: */
63: public function __construct(int $code, $input, $result, ?\Throwable $previous = null)
64: {
65: if (isset(self::$messages[$code]) !== true) {
66: throw new \InvalidArgumentException(sprintf('Encountered unknown JSON error code: [%d]', $code));
67: }
68:
69: parent::__construct(self::$messages[$code], $code, $previous);
70: $this->input = $input;
71: $this->result = $result;
72: }
73:
74: /**
75: * @return mixed
76: */
77: public function getInput()
78: {
79: return $this->input;
80: }
81:
82: /**
83: * @return mixed
84: */
85: public function getResult()
86: {
87: return $this->result;
88: }
89: }
90: