This commit is contained in:
Your Name
2021-07-26 19:46:18 +02:00
parent e7a49138bb
commit aae17f10a6
818 changed files with 70693 additions and 16406 deletions
@@ -6,35 +6,21 @@ use Illuminate\Auth\AuthenticationException as IlluminateAuthenticationException
class AuthenticationException extends IlluminateAuthenticationException implements RendersErrorsExtensions
{
/**
* Returns true when exception message is safe to be displayed to a client.
*
* @api
* @return bool
*/
public const MESSAGE = 'Unauthenticated.';
public const CATEGORY = 'authentication';
public function isClientSafe(): bool
{
return true;
}
/**
* Returns string describing a category of the error.
*
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
*
* @api
* @return string
*/
public function getCategory(): string
{
return 'authentication';
return self::CATEGORY;
}
/**
* Return the content that is put in the "extensions" part
* of the returned error.
*
* @return array
* @return array<string, array<string>>
*/
public function extensionsContent(): array
{
@@ -7,30 +7,13 @@ use Illuminate\Auth\Access\AuthorizationException as IlluminateAuthorizationExce
class AuthorizationException extends IlluminateAuthorizationException implements ClientAware
{
/**
* @var string
*/
const CATEGORY = 'authorization';
public const CATEGORY = 'authorization';
/**
* Returns true when exception message is safe to be displayed to a client.
*
* @api
* @return bool
*/
public function isClientSafe(): bool
{
return true;
}
/**
* Returns string describing a category of the error.
*
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
*
* @api
* @return string
*/
public function getCategory(): string
{
return self::CATEGORY;
@@ -5,27 +5,18 @@ namespace Nuwave\Lighthouse\Exceptions;
use Exception;
use GraphQL\Error\ClientAware;
/**
* Thrown when the schema definition or related code is wrong.
*
* This signals a developer error, so we do not show this exception to the user.
*/
class DefinitionException extends Exception implements ClientAware
{
/**
* Returns true when exception message is safe to be displayed to a client.
*
* @api
* @return bool
*/
public function isClientSafe(): bool
{
return false;
}
/**
* Returns string describing a category of the error.
*
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
*
* @api
* @return string
*/
public function getCategory(): string
{
return 'schema';
@@ -7,25 +7,11 @@ use GraphQL\Error\ClientAware;
class DirectiveException extends Exception implements ClientAware
{
/**
* Returns true when exception message is safe to be displayed to a client.
*
* @api
* @return bool
*/
public function isClientSafe(): bool
{
return false;
}
/**
* Returns string describing a category of the error.
*
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
*
* @api
* @return string
*/
public function getCategory(): string
{
return 'schema';
@@ -0,0 +1,10 @@
<?php
namespace Nuwave\Lighthouse\Exceptions;
use Exception;
class FederationException extends Exception
{
//
}
@@ -6,30 +6,20 @@ use GraphQL\Error\Error;
class GenericException extends Error
{
/**
* The category.
*
* @var string
*/
protected $category = 'generic';
/**
* Set the contents that will be rendered under the "extensions" key of the error response.
*
* @param mixed $extensions
* @param array<string, mixed> $extensions
* @return $this
*/
public function setExtensions($extensions): self
public function setExtensions(array $extensions): self
{
$this->extensions = (array) $extensions;
$this->extensions = $extensions;
return $this;
}
/**
* Set the category that will be rendered under the "extensions" key of the error response.
*
* @param string $category
* @return $this
*/
public function setCategory(string $category): self
+17 -14
View File
@@ -4,28 +4,31 @@ namespace Nuwave\Lighthouse\Exceptions;
use Exception;
use GraphQL\Error\ClientAware;
use GraphQL\Error\SyntaxError;
use GraphQL\Language\Source;
class ParseException extends Exception implements ClientAware
{
/**
* Returns true when exception message is safe to be displayed to a client.
*
* @api
* @return bool
*/
public function __construct(SyntaxError $error)
{
$message = $error->getMessage();
$source = $error->getSource();
$positions = $error->getPositions();
if ($source instanceof Source && count($positions) > 0) {
$position = $positions[0];
$message .= ', near: '.\Safe\substr($source->body, max(0, $position - 50), 100);
}
parent::__construct($message);
}
public function isClientSafe(): bool
{
return false;
}
/**
* Returns string describing a category of the error.
*
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
*
* @api
* @return string
*/
public function getCategory(): string
{
return 'schema';
@@ -0,0 +1,30 @@
<?php
namespace Nuwave\Lighthouse\Exceptions;
use GraphQL\Error\ClientAware;
use RuntimeException;
/**
* Thrown when the user has reached the rate limit for a field.
*/
class RateLimitException extends RuntimeException implements ClientAware
{
public const MESSAGE = 'Rate limit exceeded. Please try later.';
public const CATEGORY = 'rate-limit';
public function __construct()
{
parent::__construct(self::MESSAGE);
}
public function isClientSafe(): bool
{
return true;
}
public function getCategory(): string
{
return self::CATEGORY;
}
}
@@ -16,7 +16,7 @@ interface RendersErrorsExtensions extends ClientAware
* Return the content that is put in the "extensions" part
* of the returned error.
*
* @return array
* @return array<string, mixed>
*/
public function extensionsContent(): array;
}
@@ -1,35 +0,0 @@
<?php
namespace Nuwave\Lighthouse\Exceptions;
use InvalidArgumentException;
use GraphQL\Error\ClientAware;
class SubscriptionException extends InvalidArgumentException implements ClientAware
{
/**
* Returns true when exception message is safe to be displayed to a client.
*
* @api
*
* @return bool
*/
public function isClientSafe(): bool
{
return true;
}
/**
* Returns string describing a category of the error.
*
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
*
* @api
*
* @return string
*/
public function getCategory(): string
{
return 'subscription';
}
}
@@ -0,0 +1,16 @@
<?php
namespace Nuwave\Lighthouse\Exceptions;
use Exception;
class UnknownCacheVersionException extends Exception
{
/**
* @param mixed $version Should be int, but could be something else
*/
public function __construct($version)
{
parent::__construct("Expected lighthouse.cache.version to be 1 or 2, got: {$version}.");
}
}
@@ -2,36 +2,62 @@
namespace Nuwave\Lighthouse\Exceptions;
class ValidationException extends \Illuminate\Validation\ValidationException implements RendersErrorsExtensions
use Exception;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\ValidationException as LaravelValidationException;
class ValidationException extends Exception implements RendersErrorsExtensions
{
const CATEGORY = 'validation';
/**
* Returns true when exception message is safe to be displayed to a client.
*
* @return bool
* @var \Illuminate\Contracts\Validation\Validator
*/
public function isClientSafe()
protected $validator;
public function __construct(string $message, Validator $validator)
{
parent::__construct($message);
$this->validator = $validator;
}
public static function fromLaravel(LaravelValidationException $laravelException): self
{
return new static(
$laravelException->getMessage(),
$laravelException->validator
);
}
/**
* Instantiate from a plain array of messages.
*
* @see \Illuminate\Validation\ValidationException::withMessages()
*
* @param array<string, string|array<string>> $messages
*/
public static function withMessages(array $messages): self
{
return static::fromLaravel(
LaravelValidationException::withMessages($messages)
);
}
public function isClientSafe(): bool
{
return true;
}
/**
* Returns string describing a category of the error.
*
* @return string
*/
public function getCategory()
public function getCategory(): string
{
return 'validation';
return self::CATEGORY;
}
/**
* Return the content that is put in the "extensions" part
* of the returned error.
*
* @return array
*/
public function extensionsContent(): array
{
return ['validation' => $this->errors()];
return [
self::CATEGORY => $this->validator->errors()->messages(),
];
}
}