1
1
<?php
2
-
3
2
/**
4
- * Copyright © Magento, Inc. All rights reserved.
5
- * See COPYING.txt for license details .
3
+ * Copyright 2024 Adobe
4
+ * All Rights Reserved .
6
5
*/
7
-
8
6
declare (strict_types=1 );
9
7
10
8
namespace Magento \GraphQl \Controller ;
11
9
10
+ use Exception ;
12
11
use Magento \Framework \App \Area ;
13
12
use Magento \Framework \App \AreaList ;
14
13
use Magento \Framework \App \FrontControllerInterface ;
24
23
use Magento \Framework \GraphQl \Query \QueryProcessor ;
25
24
use Magento \Framework \GraphQl \Query \Resolver \ContextInterface ;
26
25
use Magento \Framework \GraphQl \Schema \SchemaGeneratorInterface ;
26
+ use Magento \Framework \GraphQl \Exception \GraphQlAuthenticationException ;
27
+ use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
27
28
use Magento \Framework \Serialize \SerializerInterface ;
28
29
use Magento \Framework \Webapi \Response ;
29
30
use Magento \GraphQl \Helper \Query \Logger \LogData ;
@@ -184,7 +185,7 @@ public function dispatch(RequestInterface $request): ResponseInterface
184
185
$ statusCode = 200 ;
185
186
$ jsonResult = $ this ->jsonFactory ->create ();
186
187
$ data = $ this ->getDataFromRequest ($ request );
187
- $ result = [];
188
+ $ result = [' errors ' => [] ];
188
189
189
190
$ schema = null ;
190
191
$ query = $ data ['query ' ] ?? '' ;
@@ -205,8 +206,14 @@ public function dispatch(RequestInterface $request): ResponseInterface
205
206
$ this ->contextFactory ->create (),
206
207
$ data ['variables ' ] ?? []
207
208
);
208
- } catch (\Exception $ error ) {
209
- $ result ['errors ' ] = isset ($ result ['errors ' ]) ? $ result ['errors ' ] : [];
209
+ $ statusCode = $ this ->getHttpResponseCode ($ result );
210
+ } catch (GraphQlAuthenticationException $ error ) {
211
+ $ result ['errors ' ][] = $ this ->graphQlError ->create ($ error );
212
+ $ statusCode = 401 ;
213
+ } catch (GraphQlAuthorizationException $ error ) {
214
+ $ result ['errors ' ][] = $ this ->graphQlError ->create ($ error );
215
+ $ statusCode = 403 ;
216
+ } catch (Exception $ error ) {
210
217
$ result ['errors ' ][] = $ this ->graphQlError ->create ($ error );
211
218
$ statusCode = ExceptionFormatter::HTTP_GRAPH_QL_SCHEMA_ERROR_STATUS ;
212
219
}
@@ -216,7 +223,7 @@ public function dispatch(RequestInterface $request): ResponseInterface
216
223
$ jsonResult ->renderResult ($ this ->httpResponse );
217
224
218
225
// log information about the query, unless it is an introspection query
219
- if (strpos ($ query , 'IntrospectionQuery ' ) === false ) {
226
+ if (! str_contains ($ query , 'IntrospectionQuery ' )) {
220
227
$ queryInformation = $ this ->logDataHelper ->getLogData ($ request , $ data , $ schema , $ this ->httpResponse );
221
228
$ this ->loggerPool ->execute ($ queryInformation );
222
229
}
@@ -247,4 +254,30 @@ private function getDataFromRequest(RequestInterface $request): array
247
254
248
255
return $ data ;
249
256
}
257
+
258
+ /**
259
+ * Retrieve http response code based on the error categories
260
+ *
261
+ * @param array $result
262
+ * @return int
263
+ */
264
+ private function getHttpResponseCode (array $ result ): int
265
+ {
266
+ if (empty ($ result ['errors ' ])) {
267
+ return 200 ;
268
+ }
269
+ foreach ($ result ['errors ' ] as $ error ) {
270
+ if (!isset ($ error ['extensions ' ]['category ' ])) {
271
+ continue ;
272
+ }
273
+ switch ($ error ['extensions ' ]['category ' ]) {
274
+ case GraphQlAuthenticationException::EXCEPTION_CATEGORY :
275
+ return 401 ;
276
+ case GraphQlAuthorizationException::EXCEPTION_CATEGORY :
277
+ return 403 ;
278
+ }
279
+ }
280
+
281
+ return 200 ;
282
+ }
250
283
}
0 commit comments