Skip to content

Commit

Permalink
fix:修复try/catch异常捕获不到错误信息和状态码
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinywan committed Jan 17, 2024
1 parent d5c21ef commit ff75216
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 38 deletions.
90 changes: 90 additions & 0 deletions src/Event/FeiShuRobotEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* @desc 飞书机器人
* @author Tinywan(ShaoBo Wan)
* @date 2022/3/21 13:55
*/

declare(strict_types=1);

namespace Tinywan\ExceptionHandler\Event;

class FeiShuRobotEvent
{
/**
* 发送机器人
* @param array $args
* @param array $config
* @param string $name
* @param string $text
* @return bool|string
*/
public static function send()
{
$accessToken = '70c44466-6fbc-4558-b454-e13c685dd18c';
$url = 'https://open.feishu.cn/open-apis/bot/v2/hook/'.$accessToken;
$timestamp = time();
$secret = '7mZrqo3Fsy5iwxKY6FMxve';//秘钥
$sign = $timestamp . "\n" . $secret;
$sign = base64_encode(hash_hmac('sha256', '', $sign, true));
// $body = [
// 'timestamp' => $timestamp,
// 'sign' => $sign,
// 'msg_type' => 'text',
// 'content' => ['text' => '这是一条测试数据这是一条测试数据这是一条测试数据']
// ];
// $postData = json_encode($data,JSON_UNESCAPED_UNICODE);
// $options = [
// 'http' => [
// 'method' => 'POST',
// 'header' => 'Content-type:application/json;charset=UTF-8',
// 'content' => $postData,
// 'timeout' => 60
// ]
// ];
// $context = stream_context_create($options);
// $result = file_get_contents($url, false, $context);

$message = '# 标题\n> 这是一个引用\n> 这是第二行';
$data = [
'timestamp' => $timestamp,
'sign' => $sign,
'msg_type' => 'post',
'content' => [
'zh_cn' => [
'title' => '测试消息markdownmarkdownmarkdownmarkdown',
'content' => [
[
[
'tag' => 'text',
'text' => '项目有更新项目有更新项目有更新项目有更新',
]
]
]
]
]
];

$jsonData = json_encode($data);

// 设置cURL选项
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
));
// 发送请求并获取响应
$response = curl_exec($ch);
var_dump($response);
if ($response === false) {
echo '请求失败: ' . curl_error($ch);
} else {
echo '请求成功';
}
curl_close($ch);
}
}
4 changes: 2 additions & 2 deletions src/Exception/BadRequestHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class BadRequestHttpException extends BaseException
/**
* @var int
*/
public $statusCode = 400;
public int $statusCode = 400;

/**
* @var string
*/
public $errorMessage = '请求参数错误,服务器不能或不会处理该请求';
public string $errorMessage = '请求参数错误,服务器不能或不会处理该请求';
}
14 changes: 7 additions & 7 deletions src/Exception/BaseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,37 @@ class BaseException extends \Exception
/**
* HTTP Response Status Code.
*/
public $statusCode = 400;
public int $statusCode = 400;

/**
* HTTP Response Header.
*/
public $header = [];
public array $header = [];

/**
* Business Error code.
*
* @var int|mixed
*/
public $errorCode = 0;
public int $errorCode = 0;

/**
* Business Error message.
* @var string
*/
public $errorMessage = 'The requested resource is not available or not exists';
public string $errorMessage = 'The requested resource is not available or not exists';

/**
* Business data.
* @var array|mixed
*/
public $data = [];
public array $data = [];

/**
* Detail Log Error message.
* @var string
*/
public $error = '';
public string $error = '';

/**
* BaseException constructor.
Expand All @@ -55,7 +55,7 @@ class BaseException extends \Exception
*/
public function __construct(string $errorMessage = '', array $params = [], string $error = '')
{
parent::__construct();
parent::__construct($errorMessage, $this->statusCode);
if (!empty($errorMessage)) {
$this->errorMessage = $errorMessage;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Exception/ForbiddenHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class ForbiddenHttpException extends BaseException
/**
* @var int
*/
public $statusCode = 403;
public int $statusCode = 403;

/**
* @link 解决跨域问题
* @var array
*/
public $header = [
public array $header = [
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Allow-Headers' => 'Authorization,Content-Type,If-Match,If-Modified-Since,If-None-Match,If-Unmodified-Since,X-Requested-With,Origin',
Expand All @@ -37,5 +37,5 @@ class ForbiddenHttpException extends BaseException
/**
* @var string
*/
public $errorMessage = '对不起,您没有该接口访问权限,请联系管理员';
public string $errorMessage = '对不起,您没有该接口访问权限,请联系管理员';
}
4 changes: 2 additions & 2 deletions src/Exception/NotFoundHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class NotFoundHttpException extends BaseException
/**
* @var int
*/
public $statusCode = 404;
public int $statusCode = 404;

/**
* @var string
*/
public $errorMessage = '未找到请求的资源';
public string $errorMessage = '未找到请求的资源';
}
10 changes: 5 additions & 5 deletions src/Exception/NotTeamMemberException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ class NotTeamMemberException extends BaseException
/**
* HTTP 状态码
*/
public $statusCode = 403;
public int $statusCode = 403;

/**
* 错误消息.
*/
public $errorMessage = '您无权访问该资源';
public string $errorMessage = '您无权访问该资源';

/**
* @var array|string[]
*/
public $data = [
'id' => 'tinywan2022',
'name' => '团队名称:开源技术小栈'
public array $data = [
'id' => 'tinywan2028',
'name' => '开源技术小栈'
];
}
4 changes: 2 additions & 2 deletions src/Exception/RouteNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class RouteNotFoundException extends BaseException
/**
* HTTP 状态码
*/
public $statusCode = 404;
public int $statusCode = 404;

/**
* 错误消息.
*/
public $errorMessage = '路由地址不存在';
public string $errorMessage = '路由地址不存在';
}
5 changes: 2 additions & 3 deletions src/Exception/ServerErrorHttpException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* @desc 服务器内部异常
*
Expand All @@ -24,10 +23,10 @@ class ServerErrorHttpException extends BaseException
/**
* @var int
*/
public $statusCode = 500;
public int $statusCode = 500;

/**
* @var string
*/
public $errorMessage = 'Internal Server Error';
public string $errorMessage = 'Internal Server Error';
}
8 changes: 4 additions & 4 deletions src/Exception/TooManyRequestsHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class TooManyRequestsHttpException extends BaseException
/**
* @var int
*/
public $statusCode = 429;
public int $statusCode = 429;

/**
* @var array
*/
public $header = [
public array $header = [
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Allow-Headers' => 'Authorization,Content-Type,If-Match,If-Modified-Since,If-None-Match,If-Unmodified-Since,X-Requested-With,Origin',
Expand All @@ -33,10 +33,10 @@ class TooManyRequestsHttpException extends BaseException
/**
* @var int
*/
public $errorCode = 0;
public int $errorCode = 0;

/**
* @var string
*/
public $errorMessage = "Too Many Requests, Please try again later";
public string $errorMessage = "Too Many Requests, Please try again later";
}
4 changes: 2 additions & 2 deletions src/Exception/UnauthorizedHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class UnauthorizedHttpException extends BaseException
/**
* HTTP 状态码
*/
public $statusCode = 401;
public int $statusCode = 401;

/**
* 错误消息.
*/
public $errorMessage = 'Unauthorized';
public string $errorMessage = 'Unauthorized';
}
16 changes: 8 additions & 8 deletions src/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,49 +39,49 @@ class Handler extends ExceptionHandler
*
* @var array
*/
public $statusCode = 200;
public int $statusCode = 200;

/**
* HTTP Response Header.
*
* @var array
*/
public $header = [];
public array $header = [];

/**
* Business Error code.
*
* @var int
*/
public $errorCode = 0;
public int $errorCode = 0;

/**
* Business Error message.
*
* @var string
*/
public $errorMessage = 'no error';
public string $errorMessage = 'no error';

/**
* 响应结果数据.
*
* @var array
*/
protected $responseData = [];
protected array $responseData = [];

/**
* config下的配置.
*
* @var array
*/
protected $config = [];
protected array $config = [];

/**
* Log Error message.
*
* @var string
*/
public $error = 'no error';
public string $error = 'no error';

/**
* @param Throwable $exception
Expand All @@ -99,7 +99,7 @@ public function report(Throwable $exception)
*/
public function render(Request $request, Throwable $exception): Response
{
$this->config = array_merge($this->config, config('plugin.tinywan.exception-handler.app.exception_handler', []));
$this->config = array_merge($this->config, config('plugin.tinywan.exception-handler.app.exception_handler', []) ?? []);

$this->addRequestInfoToResponse($request);
$this->solveAllException($exception);
Expand Down

0 comments on commit ff75216

Please sign in to comment.