@@ -14,13 +14,16 @@ class GraphQLError extends Exception
14
14
protected $ node ;
15
15
protected $ path ;
16
16
17
+ protected $ customExtensions ;
18
+
17
19
/**
18
20
* GraphQLError constructor.
19
21
* @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
22
25
*/
23
- public function __construct ($ message = "" , $ node = null , $ path = null )
26
+ public function __construct (string $ message = "" , array $ node = null , array $ path = null , array $ customExtensions = null )
24
27
{
25
28
parent ::__construct ($ message );
26
29
$ this ->node = $ node ;
@@ -30,6 +33,7 @@ function ($pathItem) {
30
33
return $ pathItem !== null ;
31
34
}
32
35
);
36
+ $ this ->customExtensions = $ customExtensions ;
33
37
}
34
38
35
39
/**
@@ -61,5 +65,37 @@ public function getErrorCode()
61
65
{
62
66
return $ this ->code ;
63
67
}
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
+ }
64
100
}
65
101
0 commit comments