php-tool-kit/src/Exceptions/ApiException.php
2019-10-31 15:58:12 +08:00

57 lines
1.2 KiB
PHP

<?php
namespace PdToolKit\Exceptions;
use Exception;
use Throwable;
use Illuminate\Http\JsonResponse;
class ApiException extends Exception
{
/**
* 返回到接口的验证码
* @var int $response_code
*/
protected $response_code = 0;
/**
* ApiException constructor.
* @param string $message
* @param int $response_code
* @param Throwable|null $previous
*/
public function __construct(
$message,
$response_code = 400000,
Throwable $previous = null
) {
parent::__construct($message, 0, $previous);
$this->response_code = $response_code;
}
public static function formException(Exception $e, $response_code = 400000)
{
return new ApiException($e->getMessage(), $response_code, $e);
}
/**
* @return int
*/
public function getResponseCode()
{
return $this->response_code;
}
/**
* @return JsonResponse
*/
public function render(): JsonResponse
{
return new JsonResponse([
'message' => $this->getMessage(),
'code' => $this->getResponseCode(),
'data' => new \stdClass(),
]);
}
}