wesmiler 5 年 前
コミット
efc8ef9194

+ 2 - 1
app/Controller/Resource/ApiResource.php

@@ -15,7 +15,7 @@ class ApiResource extends JsonResource
     public static function success($msg, $data=null)
     public static function success($msg, $data=null)
     {
     {
 
 
-        $msg = is_numeric($msg)? __('api.'.$msg) :$msg;
+        $msg = is_numeric($msg)? __('api.'.$msg) : $msg;
         $result = [
         $result = [
             'code' => __('api.1001'),
             'code' => __('api.1001'),
             'message' => $msg,
             'message' => $msg,
@@ -23,6 +23,7 @@ class ApiResource extends JsonResource
         if ($data !== null) {
         if ($data !== null) {
             $result['data'] = $data;
             $result['data'] = $data;
         }
         }
+
         return $result;
         return $result;
     }
     }
 
 

+ 0 - 24
app/Exception/ApiException.php

@@ -1,24 +0,0 @@
-<?php
-
-declare(strict_types=1);
-/**
- * API exception
- * @author wesmiler
- */
-namespace App\Exception;
-
-use App\Constants\ErrorCode;
-use Hyperf\Server\Exception\ServerException;
-use Throwable;
-
-class ApiException extends ServerException
-{
-    public function __construct(int $code = 0, string $message = null, Throwable $previous = null)
-    {
-        if (is_null($message)) {
-            $message = ErrorCode::getMessage($code);
-        }
-
-        parent::__construct($message, $code, $previous);
-    }
-}

+ 18 - 3
app/Exception/Handler/TokenValidExceptionHandler.php

@@ -10,6 +10,7 @@ namespace App\Exception\Handler;
 use Hyperf\Contract\StdoutLoggerInterface;
 use Hyperf\Contract\StdoutLoggerInterface;
 use Hyperf\ExceptionHandler\ExceptionHandler;
 use Hyperf\ExceptionHandler\ExceptionHandler;
 use Hyperf\HttpMessage\Stream\SwooleStream;
 use Hyperf\HttpMessage\Stream\SwooleStream;
+use Phper666\JWTAuth\Exception\TokenValidException;
 use Psr\Http\Message\ResponseInterface;
 use Psr\Http\Message\ResponseInterface;
 use Throwable;
 use Throwable;
 
 
@@ -27,9 +28,23 @@ class TokenValidExceptionHandler extends ExceptionHandler
 
 
     public function handle(Throwable $throwable, ResponseInterface $response)
     public function handle(Throwable $throwable, ResponseInterface $response)
     {
     {
-        $this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
-        $this->logger->error($throwable->getTraceAsString());
-        return $response->withHeader('Server', 'Hyperf')->withStatus(500)->withBody(new SwooleStream('Internal Server Error.'));
+        // 判断被捕获到的异常是希望被捕获的异常
+        if ($throwable instanceof TokenValidException) {
+            // 格式化输出
+            $message = $throwable->getMessage();
+            $message = is_numeric($message)? __('api.'.$message) : $message;
+            $data = json_encode([
+                'code' => __('api.'.$throwable->getCode()),
+                'message' => $message
+            ], 256);
+
+            // 阻止异常冒泡
+            $this->stopPropagation();
+            return $response->withStatus(500)->withBody(new SwooleStream($data));
+        }
+
+        // 交给下一个异常处理器
+        return $response;
     }
     }
 
 
     public function isValid(Throwable $throwable): bool
     public function isValid(Throwable $throwable): bool