Просмотр исходного кода

bagisto api 4XX异常添加extensions字段

fogwind 6 дней назад
Родитель
Сommit
48b9c212c1

+ 9 - 1
packages/Webkul/BagistoApi/src/Exception/AuthenticationException.php

@@ -8,7 +8,7 @@ use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
 /**
  * Thrown when a request lacks credentials. Maps to HTTP 401 in REST.
  */
-class AuthenticationException extends \Exception implements \GraphQL\Error\ClientAware, HttpExceptionInterface, ProblemExceptionInterface
+class AuthenticationException extends \Exception implements \GraphQL\Error\ClientAware, HttpExceptionInterface, ProblemExceptionInterface, \GraphQL\Error\ProvidesExtensions
 {
     private int $status = 401;
 
@@ -18,6 +18,14 @@ class AuthenticationException extends \Exception implements \GraphQL\Error\Clien
     {
         return true;
     }
+    public function getExtensions(): array
+    {
+        return [
+            'code' => 'UNAUTHENTICATED',
+            'category' => $this->getCategory(),
+            'status' => $this->getStatus()
+        ];
+    }
 
     public function getCategory(): string
     {

+ 10 - 1
packages/Webkul/BagistoApi/src/Exception/InvalidInputException.php

@@ -19,7 +19,7 @@ use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
  *
  * Status Code: 400 Bad Request
  */
-class InvalidInputException extends \Exception implements \GraphQL\Error\ClientAware, HttpExceptionInterface, ProblemExceptionInterface
+class InvalidInputException extends \Exception implements \GraphQL\Error\ClientAware, HttpExceptionInterface, ProblemExceptionInterface, \GraphQL\Error\ProvidesExtensions
 {
     private int $status = 400;
 
@@ -30,6 +30,15 @@ class InvalidInputException extends \Exception implements \GraphQL\Error\ClientA
         return true;
     }
 
+    public function getExtensions(): array
+    {
+        return [
+            'code' => 'INVALID_INPUT',
+            'category' => $this->getCategory(),
+            'status' => $this->getStatus()
+        ];
+    }
+
     public function getCategory(): string
     {
         return 'invalid_input';

+ 9 - 1
packages/Webkul/BagistoApi/src/Exception/ResourceNotFoundException.php

@@ -9,7 +9,7 @@ use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
  * Thrown when a requested resource (cart, product, item, etc.) is not found.
  * Maps to HTTP 404 in REST and an `errors[]` entry in GraphQL.
  */
-class ResourceNotFoundException extends \Exception implements \GraphQL\Error\ClientAware, HttpExceptionInterface, ProblemExceptionInterface
+class ResourceNotFoundException extends \Exception implements \GraphQL\Error\ClientAware, HttpExceptionInterface, ProblemExceptionInterface, \GraphQL\Error\ProvidesExtensions
 {
     private int $status = 404;
 
@@ -19,6 +19,14 @@ class ResourceNotFoundException extends \Exception implements \GraphQL\Error\Cli
     {
         return true;
     }
+    public function getExtensions(): array
+    {
+        return [
+            'code' => 'RESOURCE_NOT_FOUND',
+            'category' => $this->getCategory(),
+            'status' => $this->getStatus()
+        ];
+    }
 
     public function getCategory(): string
     {