49 lines
1.2 KiB
PHP
49 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Exception;
|
|
|
|
use Hyperf\Server\Exception\ServerException;
|
|
use Throwable;
|
|
|
|
class BusinessException extends ServerException
|
|
{
|
|
/**
|
|
* 业务异常.
|
|
* @param int $code 错误码
|
|
* @param string $message 错误消息
|
|
*/
|
|
public function __construct(int $code = 500, string $message = '', ?Throwable $previous = null)
|
|
{
|
|
if (empty($message)) {
|
|
$message = $this->getDefaultMessage($code);
|
|
}
|
|
|
|
parent::__construct($message, $code, $previous);
|
|
}
|
|
|
|
/**
|
|
* 获取默认错误消息.
|
|
*/
|
|
private function getDefaultMessage(int $code): string
|
|
{
|
|
$messages = [
|
|
400 => '请求参数错误',
|
|
401 => '未授权',
|
|
403 => '禁止访问',
|
|
404 => '资源不存在',
|
|
500 => '服务器内部错误',
|
|
10001 => '校区不存在',
|
|
10002 => '校区已存在',
|
|
10003 => '父级校区不存在',
|
|
10004 => '层级参数错误',
|
|
10101 => '教师不存在',
|
|
10102 => '教师已存在',
|
|
10103 => '该教师已有关联课程,无法删除',
|
|
];
|
|
|
|
return $messages[$code] ?? '业务处理失败';
|
|
}
|
|
}
|