37 lines
No EOL
783 B
PHP
37 lines
No EOL
783 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Exceptions;
|
|
|
|
use Exception;
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
class ApiException extends Exception
|
|
{
|
|
protected $statusCode = 400;
|
|
protected $errors = [];
|
|
|
|
public function __construct(string $message = '', int $code = 0, array $errors = [])
|
|
{
|
|
parent::__construct($message, $code);
|
|
$this->errors = $errors;
|
|
if ($code) {
|
|
$this->statusCode = $code;
|
|
}
|
|
}
|
|
|
|
public function render(): JsonResponse
|
|
{
|
|
$response = [
|
|
'error' => true,
|
|
'message' => $this->message,
|
|
];
|
|
|
|
if (!empty($this->errors)) {
|
|
$response['errors'] = $this->errors;
|
|
}
|
|
|
|
return response()->json($response, $this->statusCode);
|
|
}
|
|
}
|