mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-06-15 13:32:43 +01:00
51 lines
1.1 KiB
PHP
51 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Exceptions\Api;
|
|
|
|
use Exception;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use LogicException;
|
|
|
|
abstract class ApiException extends Exception
|
|
{
|
|
public const string KEY = 'api_exception';
|
|
|
|
/**
|
|
* Render the exception into an HTTP response.
|
|
*/
|
|
public function render(Request $request): JsonResponse
|
|
{
|
|
return response()
|
|
->json([
|
|
'error' => true,
|
|
'key' => $this->getKey(),
|
|
'message' => $this->getTranslatedMessage(),
|
|
], 400);
|
|
}
|
|
|
|
/**
|
|
* Get the key for the exception.
|
|
*/
|
|
public function getKey(): string
|
|
{
|
|
$key = static::KEY;
|
|
|
|
if ($key === ApiException::KEY) {
|
|
throw new LogicException('API exceptions need the KEY constant defined.');
|
|
}
|
|
|
|
return $key;
|
|
}
|
|
|
|
/**
|
|
* Get the translated message for the exception.
|
|
*/
|
|
public function getTranslatedMessage(): string
|
|
{
|
|
return __('exceptions.api.'.$this->getKey());
|
|
}
|
|
}
|