Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #665 from swoftcloud/master
Added rpc error handler
- Loading branch information
Showing
10 changed files
with
98 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,6 @@ temp/ | |
*.lock | ||
.phpintel/ | ||
.env | ||
.phpstorm.meta.php | ||
.DS_Store | ||
public/devtool/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters