| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- <?php
- declare(strict_types=1);
- namespace app\miniapi\exception;
- use app\miniapi\http\ApiResponse;
- use Webman\Http\Request;
- use Webman\Http\Response;
- /** Only mini API failures use the new contract; existing root and plugin behavior is preserved. */
- final class Handler extends \support\exception\Handler
- {
- public function report(\Throwable $exception)
- {
- $request = \request();
- if (!$request || !ApiResponse::owns($request)) {
- if (!$exception instanceof ApiException) {
- parent::report($exception);
- }
- return;
- }
- if (!$exception instanceof ApiException) {
- $this->logger->error('mini_api.error', [
- 'request_id' => ApiResponse::requestId($request),
- 'exception_type' => get_class($exception),
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'trace' => array_map(static fn ($frame) => array_intersect_key($frame, array_flip([
- 'file',
- 'line',
- 'class',
- 'function',
- ])), array_slice($exception->getTrace(), 0, 15)),
- ]);
- }
- }
- public function render(Request $request, \Throwable $exception): Response
- {
- if (!ApiResponse::owns($request)) {
- return parent::render($request, $exception);
- }
- $error = $exception instanceof ApiException ? $exception : ($exception instanceof \plugin\saiadmin\exception\ApiException
- ? new ApiException(40902, $exception->getMessage(), 409) : new ApiException(50001, '服务暂时不可用', 500));
- return ApiResponse::error($request, $error);
- }
- }
|