44 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			44 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| declare(strict_types=1);
 | |
| /**
 | |
|  * This file is part of Hyperf.
 | |
|  *
 | |
|  * @link     https://www.hyperf.io
 | |
|  * @document https://hyperf.wiki
 | |
|  * @contact  group@hyperf.io
 | |
|  * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 | |
|  */
 | |
| namespace App\Exception\Handler;
 | |
| 
 | |
| use App\Constants\ErrorCode;
 | |
| use App\Helper\Result;
 | |
| use Hyperf\Contract\StdoutLoggerInterface;
 | |
| use Hyperf\ExceptionHandler\ExceptionHandler;
 | |
| use Hyperf\HttpMessage\Stream\SwooleStream;
 | |
| use Psr\Http\Message\ResponseInterface;
 | |
| use Throwable;
 | |
| 
 | |
| class AppExceptionHandler extends ExceptionHandler
 | |
| {
 | |
|     public function __construct(protected StdoutLoggerInterface $logger)
 | |
|     {
 | |
|     }
 | |
| 
 | |
|     public function handle(Throwable $throwable, ResponseInterface $response)
 | |
|     {
 | |
|         $message = $throwable->getMessage();
 | |
|         $error = explode('|', $message, 2);
 | |
|         $code = $error[1] ?? $throwable->getCode() ?: ErrorCode::COMMON_ERROR;
 | |
|         $data = Result::error($code, $error[0]);
 | |
|         $responseStr = json_encode($data, JSON_UNESCAPED_UNICODE);
 | |
| 
 | |
|         return $response->withHeader('Content-Type', 'application/json')->withBody(new SwooleStream($responseStr));
 | |
|     }
 | |
| 
 | |
|     public function isValid(Throwable $throwable): bool
 | |
|     {
 | |
|         return true;
 | |
|     }
 | |
| }
 |