@@ -14,13 +14,16 @@ class GraphQLError extends Exception
1414 protected $ node ;
1515 protected $ path ;
1616
17+ protected $ customExtensions ;
18+
1719 /**
1820 * GraphQLError constructor.
1921 * @param string $message
20- * @param null $node
21- * @param null $path
22+ * @param array|null $node
23+ * @param array|null $path
24+ * @param array|null $customExtensions
2225 */
23- public function __construct ($ message = "" , $ node = null , $ path = null )
26+ public function __construct (string $ message = "" , array $ node = null , array $ path = null , array $ customExtensions = null )
2427 {
2528 parent ::__construct ($ message );
2629 $ this ->node = $ node ;
@@ -30,6 +33,7 @@ function ($pathItem) {
3033 return $ pathItem !== null ;
3134 }
3235 );
36+ $ this ->customExtensions = $ customExtensions ;
3337 }
3438
3539 /**
@@ -61,5 +65,37 @@ public function getErrorCode()
6165 {
6266 return $ this ->code ;
6367 }
68+
69+ /**
70+ * @param string $key
71+ * @param mixed $value
72+ * @return $this
73+ */
74+ public function addCustomExtension (string $ key , $ value ): GraphQLError
75+ {
76+ $ this ->customExtensions [$ key ] = $ value ;
77+ return $ this ;
78+ }
79+
80+ /**
81+ * Returns all added custom extensions
82+ * @return array
83+ */
84+ public function getCustomExtensions (): array
85+ {
86+ return $ this ->customExtensions ?? [];
87+ }
88+
89+ /**
90+ * Returns all extensions of the error.
91+ *
92+ * @return array
93+ */
94+ public function getExtensions (): array
95+ {
96+ return array_merge ([
97+ "code " => $ this ->getErrorCode ()
98+ ], $ this ->getCustomExtensions ());
99+ }
64100}
65101
0 commit comments