Loading src/Exceptions/Handler/MicroExceptionHandler.php 0 → 100644 +57 −0 Original line number Diff line number Diff line <?php /** * Created by PhpStorm. * User: zero * Date: 2020/2/7 * Time: 16:39 */ namespace Meibuyu\Micro\Exceptions\Handler; use Hyperf\ExceptionHandler\ExceptionHandler; use Hyperf\HttpMessage\Stream\SwooleStream; use Meibuyu\Micro\Exceptions\HttpResponseException; use Meibuyu\Micro\Exceptions\ValidatorException; use Psr\Http\Message\ResponseInterface; use Throwable; class MicroExceptionHandler extends ExceptionHandler { public function handle(Throwable $throwable, ResponseInterface $response) { // 判断被捕获到的异常是希望被捕获的异常 if ($throwable instanceof HttpResponseException) { // 格式化输出 $data = json_encode([ 'code' => $throwable->getCode() ?: 400, 'msg' => $throwable->getMessage(), ], JSON_UNESCAPED_UNICODE); // 阻止异常冒泡 $this->stopPropagation(); return $response ->withAddedHeader('content-type', 'application/json') ->withBody(new SwooleStream($data)); } else if ($throwable instanceof ValidatorException) { $this->stopPropagation(); // 阻止异常冒泡 /** @var ValidatorException $throwable */ $data = json_encode([ 'code' => $throwable->getCode() ?: 401, 'msg' => $throwable->first(), ], JSON_UNESCAPED_UNICODE); return $response ->withAddedHeader('content-type', 'application/json') ->withBody(new SwooleStream($data)); } return $response; // 交给下一个异常处理器 } public function isValid(Throwable $throwable): bool { return true; } } No newline at end of file src/Exceptions/HttpResponseException.php 0 → 100644 +16 −0 Original line number Diff line number Diff line <?php /** * Created by PhpStorm. * User: zero * Date: 2020/2/7 * Time: 16:18 */ namespace Meibuyu\Micro\Exceptions; class HttpResponseException extends \Exception { } No newline at end of file src/Exceptions/RepositoryException.php 0 → 100644 +16 −0 Original line number Diff line number Diff line <?php /** * Created by PhpStorm. * User: zero * Date: 2020/2/7 * Time: 15:24 */ namespace Meibuyu\Micro\Exceptions; class RepositoryException extends \Exception { } No newline at end of file src/Validator/Exceptions/ValidatorException.php→src/Exceptions/ValidatorException.php +1 −1 Original line number Diff line number Diff line <?php namespace Meibuyu\Micro\Validator\Exceptions; namespace Meibuyu\Micro\Exceptions; use Hyperf\Utils\Contracts\MessageBag; Loading src/Validator/AbstractValidator.php +1 −1 Original line number Diff line number Diff line Loading @@ -3,8 +3,8 @@ namespace Meibuyu\Micro\Validator; use Hyperf\Utils\MessageBag; use Meibuyu\Micro\Exceptions\ValidatorException; use Meibuyu\Micro\Validator\Contracts\ValidatorInterface; use Meibuyu\Micro\Validator\Exceptions\ValidatorException; abstract class AbstractValidator implements ValidatorInterface { Loading Loading
src/Exceptions/Handler/MicroExceptionHandler.php 0 → 100644 +57 −0 Original line number Diff line number Diff line <?php /** * Created by PhpStorm. * User: zero * Date: 2020/2/7 * Time: 16:39 */ namespace Meibuyu\Micro\Exceptions\Handler; use Hyperf\ExceptionHandler\ExceptionHandler; use Hyperf\HttpMessage\Stream\SwooleStream; use Meibuyu\Micro\Exceptions\HttpResponseException; use Meibuyu\Micro\Exceptions\ValidatorException; use Psr\Http\Message\ResponseInterface; use Throwable; class MicroExceptionHandler extends ExceptionHandler { public function handle(Throwable $throwable, ResponseInterface $response) { // 判断被捕获到的异常是希望被捕获的异常 if ($throwable instanceof HttpResponseException) { // 格式化输出 $data = json_encode([ 'code' => $throwable->getCode() ?: 400, 'msg' => $throwable->getMessage(), ], JSON_UNESCAPED_UNICODE); // 阻止异常冒泡 $this->stopPropagation(); return $response ->withAddedHeader('content-type', 'application/json') ->withBody(new SwooleStream($data)); } else if ($throwable instanceof ValidatorException) { $this->stopPropagation(); // 阻止异常冒泡 /** @var ValidatorException $throwable */ $data = json_encode([ 'code' => $throwable->getCode() ?: 401, 'msg' => $throwable->first(), ], JSON_UNESCAPED_UNICODE); return $response ->withAddedHeader('content-type', 'application/json') ->withBody(new SwooleStream($data)); } return $response; // 交给下一个异常处理器 } public function isValid(Throwable $throwable): bool { return true; } } No newline at end of file
src/Exceptions/HttpResponseException.php 0 → 100644 +16 −0 Original line number Diff line number Diff line <?php /** * Created by PhpStorm. * User: zero * Date: 2020/2/7 * Time: 16:18 */ namespace Meibuyu\Micro\Exceptions; class HttpResponseException extends \Exception { } No newline at end of file
src/Exceptions/RepositoryException.php 0 → 100644 +16 −0 Original line number Diff line number Diff line <?php /** * Created by PhpStorm. * User: zero * Date: 2020/2/7 * Time: 15:24 */ namespace Meibuyu\Micro\Exceptions; class RepositoryException extends \Exception { } No newline at end of file
src/Validator/Exceptions/ValidatorException.php→src/Exceptions/ValidatorException.php +1 −1 Original line number Diff line number Diff line <?php namespace Meibuyu\Micro\Validator\Exceptions; namespace Meibuyu\Micro\Exceptions; use Hyperf\Utils\Contracts\MessageBag; Loading
src/Validator/AbstractValidator.php +1 −1 Original line number Diff line number Diff line Loading @@ -3,8 +3,8 @@ namespace Meibuyu\Micro\Validator; use Hyperf\Utils\MessageBag; use Meibuyu\Micro\Exceptions\ValidatorException; use Meibuyu\Micro\Validator\Contracts\ValidatorInterface; use Meibuyu\Micro\Validator\Exceptions\ValidatorException; abstract class AbstractValidator implements ValidatorInterface { Loading