Skip to content

Commit

Permalink
Merge pull request #665 from swoftcloud/master
Browse files Browse the repository at this point in the history
Added rpc error handler
  • Loading branch information
stelin committed May 28, 2019
2 parents b61b489 + 7f9d26c commit 42e6b92
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -10,5 +10,6 @@ temp/
*.lock
.phpintel/
.env
.phpstorm.meta.php
.DS_Store
public/devtool/
13 changes: 1 addition & 12 deletions app/Exception/ApiException.php
@@ -1,21 +1,10 @@
<?php
/**
* +----------------------------------------------------------------------
* | 自定义异常
* +----------------------------------------------------------------------
* | Copyright (c) 2019 http://www.sunnyos.com All rights reserved.
* +----------------------------------------------------------------------
* | Date:2019-05-22 15:58:14
* | Author: Sunny (admin@mail.sunnyos.com) QQ:327388905
* +----------------------------------------------------------------------
*/

namespace App\Exception;

/**
* Class ApiException
*
* @sunny 2.0
* @since 2.0
*/
class ApiException extends \Exception
{
Expand Down
2 changes: 1 addition & 1 deletion app/Exception/Handler/ApiExceptionHandler.php
Expand Up @@ -12,7 +12,7 @@
/**
* Class ApiExceptionHandler
*
* @sunny 2.0
* @since 2.0
*
* @ExceptionHandler(ApiException::class)
*/
Expand Down
51 changes: 51 additions & 0 deletions app/Exception/Handler/RpcExceptionHandler.php
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);


namespace App\Exception\Handler;

use ReflectionException;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Error\Annotation\Mapping\ExceptionHandler;
use Swoft\Log\Debug;
use Swoft\Rpc\Error;
use Swoft\Rpc\Server\Exception\Handler\RpcErrorHandler;
use Swoft\Rpc\Server\Response;
use Throwable;


/**
* Class RpcExceptionHandler
*
* @since 2.0
*
* @ExceptionHandler(\Throwable::class)
*/
class RpcExceptionHandler extends RpcErrorHandler
{

/**
* @param Throwable $e
* @param Response $response
*
* @return Response
* @throws ReflectionException
* @throws ContainerException
*/
public function handle(Throwable $e, Response $response): Response
{
// Debug is false
if (!APP_DEBUG) {
$message = sprintf(' %s At %s line %d', $e->getMessage(), $e->getFile(), $e->getLine());
$error = Error::new($e->getCode(), $message, null);
} else {
$error = Error::new($e->getCode(), $e->getMessage(), null);
}

Debug::log('Rpc server error(%s)', $e->getMessage());

$response->setError($error);

// Debug is true
return $response;
}
}
13 changes: 2 additions & 11 deletions app/Http/Controller/ExceptionController.php
@@ -1,15 +1,4 @@
<?php
/**
* +----------------------------------------------------------------------
* | 异常demo
* +----------------------------------------------------------------------
* | Copyright (c) 2019 http://www.sunnyos.com All rights reserved.
* +----------------------------------------------------------------------
* | Date:2019-05-22 16:01:33
* | Author: Sunny (admin@mail.sunnyos.com) QQ:327388905
* +----------------------------------------------------------------------
*/

namespace App\Http\Controller;

use App\Exception\ApiException;
Expand All @@ -23,6 +12,8 @@ class ExceptionController
{
/**
* @RequestMapping(route="api")
*
* @throws ApiException
*/
public function api(){
throw new ApiException("api of ExceptionController");
Expand Down
18 changes: 16 additions & 2 deletions app/Http/Controller/RpcController.php
Expand Up @@ -4,7 +4,7 @@
namespace App\Http\Controller;

use App\Rpc\Lib\UserInterface;
use Swoft\Bean\Annotation\Mapping\Bean;
use Exception;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Rpc\Client\Annotation\Mapping\Reference;
Expand Down Expand Up @@ -68,8 +68,22 @@ public function returnBool(): array
*/
public function bigString(): array
{
$string = $this->userService->getBigContent();
$this->userService->getBigContent();

return ['string'];
}

/**
* @RequestMapping()
*
* @return array
*
* @throws Exception
*/
public function exception(): array
{
$this->userService->exception();

return ['exception'];
}
}
5 changes: 5 additions & 0 deletions app/Rpc/Lib/UserInterface.php
Expand Up @@ -30,4 +30,9 @@ public function delete(int $id): bool;
* @return string
*/
public function getBigContent(): string;

/**
* Exception
*/
public function exception(): void;
}
10 changes: 10 additions & 0 deletions app/Rpc/Service/UserService.php
Expand Up @@ -5,6 +5,7 @@


use App\Rpc\Lib\UserInterface;
use Exception;
use Swoft\Co;
use Swoft\Rpc\Server\Annotation\Mapping\Service;

Expand Down Expand Up @@ -47,4 +48,13 @@ public function getBigContent(): string
$content = Co::readFile(__DIR__ . '/big.data');
return $content;
}

/**
* Exception
* @throws Exception
*/
public function exception(): void
{
throw new Exception('exception version');
}
}
10 changes: 10 additions & 0 deletions app/Rpc/Service/UserServiceV2.php
Expand Up @@ -5,6 +5,7 @@


use App\Rpc\Lib\UserInterface;
use Exception;
use Swoft\Co;
use Swoft\Rpc\Server\Annotation\Mapping\Service;

Expand Down Expand Up @@ -50,4 +51,13 @@ public function getBigContent(): string
$content = Co::readFile(__DIR__ . '/big.data');
return $content;
}

/**
* Exception
* @throws Exception
*/
public function exception(): void
{
throw new Exception('exception version2');
}
}
2 changes: 1 addition & 1 deletion app/bean.php
Expand Up @@ -36,7 +36,7 @@
],
'db' => [
'class' => Database::class,
'dsn' => 'mysql:dbname=test;host=127.0.0.1',
'dsn' => 'mysql:dbname=test;host=172.17.0.3',
'username' => 'root',
'password' => 'swoft123456',
],
Expand Down

0 comments on commit 42e6b92

Please sign in to comment.