mirror of
https://devops.lemonos.cn/lawson/FendxPHP.git
synced 2026-06-15 23:12:49 +08:00
34 lines
1020 B
PHP
34 lines
1020 B
PHP
|
|
<?php
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Fendx\Common\Exception;
|
||
|
|
|
||
|
|
class BusinessException extends BaseException
|
||
|
|
{
|
||
|
|
public function __construct(int $code, string $messageKey, array $data = [], ?\Throwable $previous = null)
|
||
|
|
{
|
||
|
|
$message = $this->getMessageByKey($messageKey, $data);
|
||
|
|
parent::__construct($message, $code, $previous, $data);
|
||
|
|
}
|
||
|
|
|
||
|
|
private function getMessageByKey(string $key, array $data = []): string
|
||
|
|
{
|
||
|
|
$messages = [
|
||
|
|
'DB_CONNECT_FAILED' => '数据库连接失败: {message}',
|
||
|
|
'VALIDATION_FAILED' => '参数验证失败',
|
||
|
|
'UNAUTHORIZED' => '未授权访问',
|
||
|
|
'FORBIDDEN' => '禁止访问',
|
||
|
|
'NOT_FOUND' => '资源不存在',
|
||
|
|
'SERVER_ERROR' => '服务器内部错误',
|
||
|
|
];
|
||
|
|
|
||
|
|
$message = $messages[$key] ?? $key;
|
||
|
|
|
||
|
|
foreach ($data as $k => $v) {
|
||
|
|
$message = str_replace('{' . $k . '}', (string)$v, $message);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $message;
|
||
|
|
}
|
||
|
|
}
|