63 lines
1.3 KiB
PHP
63 lines
1.3 KiB
PHP
<?php
|
||
|
||
namespace App\Exceptions;
|
||
|
||
use Exception;
|
||
use Illuminate\Http\JsonResponse;
|
||
|
||
class ApiException extends Exception
|
||
{
|
||
/**
|
||
* APIエラーコード
|
||
*/
|
||
protected string $errorCode;
|
||
|
||
/**
|
||
* HTTPステータスコード
|
||
*/
|
||
protected int $httpStatus;
|
||
|
||
/**
|
||
* コンストラクタ
|
||
*
|
||
* @param string $errorCode APIエラーコード(E01, E02, etc.)
|
||
* @param string $message エラーメッセージ
|
||
* @param int $httpStatus HTTPステータスコード
|
||
*/
|
||
public function __construct(string $errorCode, string $message, int $httpStatus = 400)
|
||
{
|
||
parent::__construct($message);
|
||
$this->errorCode = $errorCode;
|
||
$this->httpStatus = $httpStatus;
|
||
}
|
||
|
||
/**
|
||
* エラーコード取得
|
||
*/
|
||
public function getErrorCode(): string
|
||
{
|
||
return $this->errorCode;
|
||
}
|
||
|
||
/**
|
||
* HTTPステータス取得
|
||
*/
|
||
public function getHttpStatus(): int
|
||
{
|
||
return $this->httpStatus;
|
||
}
|
||
|
||
/**
|
||
* JSONレスポンスを生成
|
||
*/
|
||
public function render(): JsonResponse
|
||
{
|
||
return response()->json([
|
||
'error' => [
|
||
'code' => $this->errorCode,
|
||
'message' => $this->getMessage()
|
||
]
|
||
], $this->httpStatus);
|
||
}
|
||
}
|