api.so-manager-dev.com/app/Exceptions/ApiException.php
Your Name 0b4acd7475
All checks were successful
Deploy api / deploy (push) Successful in 22s
Batch & API
2026-01-16 19:28:13 +09:00

63 lines
1.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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);
}
}