Skip to content

Commit

Permalink
refactor(exceptions): rename
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Dec 28, 2024
1 parent 1ed4db7 commit f51eeb2
Show file tree
Hide file tree
Showing 21 changed files with 65 additions and 76 deletions.
8 changes: 4 additions & 4 deletions src/Abstracts/Requests/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Apiato\Core\Abstracts\Requests;

use Apiato\Core\Abstracts\Models\UserModel as User;
use Apiato\Core\Exceptions\IncorrectIdException;
use Apiato\Core\Exceptions\IncorrectId;
use Apiato\Core\Traits\HashIdTrait;
use Apiato\Core\Traits\SanitizerTrait;
use Illuminate\Foundation\Http\FormRequest as LaravelRequest;
Expand Down Expand Up @@ -171,7 +171,7 @@ protected function hasAnyRoleAccess($user): array
* Note that the old value (data.attributes.name) is removed the original request - this method manipulates the request!
* Be sure you know what you do!
*
* @throws IncorrectIdException
* @throws IncorrectId
* @throws \Throwable
*/
public function mapInput(array $fields): void
Expand Down Expand Up @@ -199,7 +199,7 @@ public function mapInput(array $fields): void
*
* @param null $keys
*
* @throws IncorrectIdException
* @throws IncorrectId
* @throws \Throwable
*/
public function all($keys = null): array
Expand Down Expand Up @@ -233,7 +233,7 @@ protected function mergeUrlParametersWithRequestData(array $requestData): array
/**
* This method mimics the $request->input() method but works on the "decoded" values.
*
* @throws IncorrectIdException
* @throws IncorrectId
* @throws \Throwable
*/
public function getInputByKey($key = null, $default = null): mixed
Expand Down
8 changes: 4 additions & 4 deletions src/Abstracts/Transformers/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Apiato\Core\Abstracts\Transformers;

use Apiato\Core\Exceptions\CoreInternalErrorException;
use Apiato\Core\Exceptions\UnsupportedFractalIncludeException;
use Apiato\Core\Exceptions\InternalError;
use Apiato\Core\Exceptions\UnsupportedInclude;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\Primitive;
Expand Down Expand Up @@ -47,10 +47,10 @@ protected function callIncludeMethod(Scope $scope, $includeName, $data)
return parent::callIncludeMethod($scope, $includeName, $data);
} catch (\ErrorException $exception) {
if (config('apiato.requests.force-valid-includes', true)) {
throw new UnsupportedFractalIncludeException($exception->getMessage());
throw new UnsupportedInclude($exception->getMessage());
}
} catch (\Exception $exception) {
throw new CoreInternalErrorException($exception->getMessage());
throw new InternalError($exception->getMessage());
}
}

Expand Down
12 changes: 0 additions & 12 deletions src/Exceptions/AuthenticationException.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Apiato\Core\Abstracts\Exceptions\Exception;
use Symfony\Component\HttpFoundation\Response;

class GeneratorErrorException extends Exception
class GeneratorError extends Exception
{
protected $code = Response::HTTP_BAD_REQUEST;
protected $message = 'Generator Error.';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Apiato\Core\Abstracts\Exceptions\Exception;
use Symfony\Component\HttpFoundation\Response;

class IncorrectIdException extends Exception
class IncorrectId extends Exception
{
protected $code = Response::HTTP_BAD_REQUEST;
protected $message = 'Incorrect value. consider using the hashed ID.';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Apiato\Core\Abstracts\Exceptions\Exception;
use Symfony\Component\HttpFoundation\Response;

class CoreInternalErrorException extends Exception
class InternalError extends Exception
{
protected $code = Response::HTTP_INTERNAL_SERVER_ERROR;
protected $message = 'Something went wrong!';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Apiato\Core\Abstracts\Transformers\Transformer;
use Symfony\Component\HttpFoundation\Response;

class InvalidTransformerException extends Exception
class InvalidTransformer extends Exception
{
protected $code = Response::HTTP_INTERNAL_SERVER_ERROR;
protected $message = 'Transformers must extended the ' . Transformer::class . ' class.';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Apiato\Core\Abstracts\Exceptions\Exception;
use Symfony\Component\HttpFoundation\Response;

class MissingJSONHeaderException extends Exception
class MissingJSONHeader extends Exception
{
protected $code = Response::HTTP_BAD_REQUEST;
protected $message = 'Your request must contain [Accept = application/json] header.';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Apiato\Core\Abstracts\Exceptions\Exception;
use Symfony\Component\HttpFoundation\Response;

class MissingTestEndpointException extends Exception
class MissingTestEndpoint extends Exception
{
protected $code = Response::HTTP_INTERNAL_SERVER_ERROR;
protected $message = 'Property ($this->endpoint) is missed in your test.';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Apiato\Core\Abstracts\Exceptions\Exception;
use Symfony\Component\HttpFoundation\Response;

class UndefinedMethodException extends Exception
class UndefinedMethod extends Exception
{
protected $code = Response::HTTP_FORBIDDEN;
protected $message = 'Undefined HTTP Verb!';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Apiato\Core\Abstracts\Exceptions\Exception;
use Symfony\Component\HttpFoundation\Response;

class UnsupportedFractalIncludeException extends Exception
class UnsupportedInclude extends Exception
{
protected $code = Response::HTTP_BAD_REQUEST;
protected $message = 'Requested a invalid Include Parameter.';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Apiato\Core\Abstracts\Exceptions\Exception;
use Symfony\Component\HttpFoundation\Response;

class WrongEndpointFormatException extends Exception
class WrongEndpointFormat extends Exception
{
protected $code = Response::HTTP_INTERNAL_SERVER_ERROR;
protected $message = 'tests ($this->endpoint) property must be formatted as "verb@url".';
Expand Down
8 changes: 4 additions & 4 deletions src/Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Apiato\Core\Generator;

use Apiato\Core\Exceptions\GeneratorErrorException;
use Apiato\Core\Exceptions\GeneratorError;
use Apiato\Core\Generator\Interfaces\ComponentsGenerator;
use Apiato\Core\Generator\Traits\FileSystemTrait;
use Apiato\Core\Generator\Traits\FormatterTrait;
Expand Down Expand Up @@ -90,7 +90,7 @@ public function __construct(IlluminateFilesystem $fileSystem)
/**
* @void
*
* @throws GeneratorErrorException|FileNotFoundException
* @throws GeneratorError|FileNotFoundException
*/
public function handle()
{
Expand Down Expand Up @@ -138,12 +138,12 @@ public function handle()
}

/**
* @throws GeneratorErrorException
* @throws GeneratorError
*/
private function validateGenerator($generator): void
{
if (!$generator instanceof ComponentsGenerator) {
throw new GeneratorErrorException('Your component maker command should implement ComponentsGenerator interface.');
throw new GeneratorError('Your component maker command should implement ComponentsGenerator interface.');
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Middlewares/Http/ValidateJsonContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
namespace Apiato\Core\Middlewares\Http;

use Apiato\Core\Abstracts\Middlewares\Middleware;
use Apiato\Core\Exceptions\MissingJSONHeaderException;
use Apiato\Core\Exceptions\MissingJSONHeader;
use Illuminate\Http\Request;

class ValidateJsonContent extends Middleware
{
/**
* @throws MissingJSONHeaderException
* @throws MissingJSONHeader
*/
public function handle(Request $request, \Closure $next)
{
if (!$request->expectsJson() && config('apiato.requests.force-accept-header')) {
throw new MissingJSONHeaderException();
throw new MissingJSONHeader();
}

return $next($request);
Expand Down
8 changes: 4 additions & 4 deletions src/Traits/CanOwnTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Apiato\Core\Traits;

use Apiato\Core\Exceptions\CoreInternalErrorException;
use Apiato\Core\Exceptions\InternalError;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

Expand All @@ -18,7 +18,7 @@ trait CanOwnTrait
*
* If the relation name is different, you can pass it as the second parameter.
*
* @throws CoreInternalErrorException
* @throws InternalError
*/
public function isOwnedBy(Model $owner, string|null $relation = null): bool
{
Expand All @@ -35,7 +35,7 @@ public function isOwnedBy(Model $owner, string|null $relation = null): bool
*
* If the relation name is different, you can pass it as the second parameter.
*
* @throws CoreInternalErrorException
* @throws InternalError
*/
public function owns(Model $ownable, string|null $relation = null): bool
{
Expand All @@ -53,7 +53,7 @@ public function owns(Model $ownable, string|null $relation = null): bool
return null !== $this->$relation()->find($ownable);
}

throw new CoreInternalErrorException('No relationship found. Please pass the relationship name as the second parameter.');
throw new InternalError('No relationship found. Please pass the relationship name as the second parameter.');
}

protected function guessSingularRelationshipName(Model $ownable): string
Expand Down
20 changes: 10 additions & 10 deletions src/Traits/HashIdTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Apiato\Core\Traits;

use Apiato\Core\Exceptions\CoreInternalErrorException;
use Apiato\Core\Exceptions\IncorrectIdException;
use Apiato\Core\Exceptions\InternalError;
use Apiato\Core\Exceptions\IncorrectId;
use Vinkla\Hashids\Facades\Hashids;

trait HashIdTrait
Expand All @@ -26,7 +26,7 @@ trait HashIdTrait
public function getHashedKey(string|null $field = null): string|null
{
// if no key is set, use the default key name (i.e., id)
if (null === $field) {
if (is_null($field)) {
$field = $this->getKeyName();
}

Expand All @@ -35,7 +35,7 @@ public function getHashedKey(string|null $field = null): string|null
// we need to get the VALUE for this KEY (model field)
$value = $this->getAttribute($field);

if (null === $value) {
if (is_null($value)) {
return null;
}

Expand Down Expand Up @@ -77,7 +77,7 @@ public function decodeArray(array $ids): array
public function decode(string|null $id): int|null
{
// check if passed as null, (could be an optional decodable variable)
if (null === $id || 'null' === strtolower($id)) {
if (is_null($id) || 'null' === strtolower($id)) {
return $id;
}

Expand All @@ -99,7 +99,7 @@ private function decoder($id): array
* without decoding the encoded ID's you won't be able to use
* validation features like `exists:table,id`.
*
* @throws IncorrectIdException
* @throws IncorrectId
* @throws \Throwable
*/
protected function decodeHashedIdsBeforeValidation(array $requestData): array
Expand All @@ -118,7 +118,7 @@ protected function decodeHashedIdsBeforeValidation(array $requestData): array
/**
* Search the IDs to be decoded in the request data.
*
* @throws IncorrectIdException
* @throws IncorrectId
* @throws \Throwable
*/
private function locateAndDecodeIds($requestData, $key): mixed
Expand All @@ -133,7 +133,7 @@ private function locateAndDecodeIds($requestData, $key): mixed
/**
* Recursive function to process (decode) the request data with a given key.
*
* @throws IncorrectIdException
* @throws IncorrectId
* @throws \Throwable
*/
private function processField($data, $keysTodo, $currentFieldName): mixed
Expand All @@ -152,13 +152,13 @@ private function processField($data, $keysTodo, $currentFieldName): mixed

throw_if(
!is_string($data),
new CoreInternalErrorException('String expected, got ' . gettype($data), 422),
new InternalError('String expected, got ' . gettype($data), 422),
);

$decodedField = $this->decode($data);

if (is_null($decodedField)) {
throw new IncorrectIdException('ID (' . $currentFieldName . ') is incorrect, consider using the hashed ID.');
throw new IncorrectId('ID (' . $currentFieldName . ') is incorrect, consider using the hashed ID.');
}

return $decodedField;
Expand Down
6 changes: 3 additions & 3 deletions src/Traits/HashedRouteBindingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Apiato\Core\Traits;

use Apiato\Core\Exceptions\IncorrectIdException;
use Apiato\Core\Exceptions\IncorrectId;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Str;
Expand All @@ -15,14 +15,14 @@ trait HashedRouteBindingTrait
* @param Model|Relation $query
* @param string|null $field
*
* @throws IncorrectIdException
* @throws IncorrectId
*/
public function resolveRouteBindingQuery($query, $value, $field = null)
{
if (config('apiato.hash-id')) {
$decodingResult = $this->decode($value);
if (is_null($decodingResult)) {
throw new IncorrectIdException();
throw new IncorrectId();
}
$value = $decodingResult;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Traits/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Apiato\Core\Abstracts\Models\Model;
use Apiato\Core\Abstracts\Transformers\Transformer;
use Apiato\Core\Exceptions\InvalidTransformerException;
use Apiato\Core\Exceptions\InvalidTransformer;
use Illuminate\Http\JsonResponse;
use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Support\Collection;
Expand All @@ -16,7 +16,7 @@ trait ResponseTrait
protected array $metaData = [];

/**
* @throws InvalidTransformerException
* @throws InvalidTransformer
*/
public function transform(
$data,
Expand All @@ -36,7 +36,7 @@ public function transform(

// now, finally check, if the class is really a TRANSFORMER
if (!($transformer instanceof Transformer)) {
throw new InvalidTransformerException();
throw new InvalidTransformer();
}

// add specific meta information to the response message
Expand Down
5 changes: 3 additions & 2 deletions src/Traits/SanitizerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Apiato\Core\Traits;

use Apiato\Core\Exceptions\IncorrectIdException;
use Apiato\Core\Exceptions\IncorrectId;
use Illuminate\Support\Arr;

trait SanitizerTrait
Expand All @@ -15,7 +15,8 @@ trait SanitizerTrait
*
* @return array an array containing the values if the field was present in the request and the intersection array
*
* @throws IncorrectIdException
* @throws IncorrectId
* @throws \Throwable
*/
public function sanitizeInput(array $fields): array
{
Expand Down
Loading

0 comments on commit f51eeb2

Please sign in to comment.