Skip to content

Commit

Permalink
异常类可以通过定义AlwaysErrorMsg注解 明确指定是否需要输出错误信息
Browse files Browse the repository at this point in the history
该注解方式不受show_error_msg配置影响
  • Loading branch information
liu21st committed Jul 17, 2024
1 parent 4594fae commit 4587519
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/think/exception/AlwaysErrorMsg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace think\exception;

use Attribute;

/**
* 异常类可以定义类的注解属性 则始终输出错误信息
*/
#[Attribute(Attribute::TARGET_CLASS)]
final class AlwaysErrorMsg
{
}
6 changes: 5 additions & 1 deletion src/think/exception/Handle.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace think\exception;

use Exception;
use ReflectionClass;
use think\App;
use think\console\Output;
use think\db\exception\DataNotFoundException;
Expand Down Expand Up @@ -182,7 +183,10 @@ protected function convertExceptionToArray(Throwable $exception): array
'message' => $this->getMessage($exception),
];

if (!$this->app->config->get('app.show_error_msg')) {
$reflectionClass = new ReflectionClass($exception);
$alwaysMsg = $reflectionClass->getAttributes(AlwaysErrorMsg::class);

if (empty($alwaysMsg) && !$this->app->config->get('app.show_error_msg')) {
// 不显示详细错误信息
$data['message'] = $this->app->config->get('app.error_message');
}
Expand Down

0 comments on commit 4587519

Please sign in to comment.