first commit
This commit is contained in:
1
Swoft_BackEndApi/app/Exception/.pydio
Normal file
1
Swoft_BackEndApi/app/Exception/.pydio
Normal file
@@ -0,0 +1 @@
|
||||
2b50acd1-0aa8-447e-8c7d-b456aca47f0c
|
||||
12
Swoft_BackEndApi/app/Exception/ApiException.php
Executable file
12
Swoft_BackEndApi/app/Exception/ApiException.php
Executable file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace App\Exception;
|
||||
|
||||
/**
|
||||
* Class ApiException
|
||||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
class ApiException extends \Exception
|
||||
{
|
||||
|
||||
}
|
||||
1
Swoft_BackEndApi/app/Exception/Handler/.pydio
Normal file
1
Swoft_BackEndApi/app/Exception/Handler/.pydio
Normal file
@@ -0,0 +1 @@
|
||||
988cd99e-1f40-4960-a441-70636b490308
|
||||
37
Swoft_BackEndApi/app/Exception/Handler/ApiExceptionHandler.php
Executable file
37
Swoft_BackEndApi/app/Exception/Handler/ApiExceptionHandler.php
Executable file
@@ -0,0 +1,37 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Exception\Handler;
|
||||
|
||||
|
||||
use App\Exception\ApiException;
|
||||
use Swoft\Error\Annotation\Mapping\ExceptionHandler;
|
||||
use Swoft\Http\Message\Response;
|
||||
use Swoft\Http\Server\Exception\Handler\AbstractHttpErrorHandler;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Class ApiExceptionHandler
|
||||
*
|
||||
* @since 2.0
|
||||
*
|
||||
* @ExceptionHandler(ApiException::class)
|
||||
*/
|
||||
class ApiExceptionHandler extends AbstractHttpErrorHandler
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Throwable $except
|
||||
* @param Response $response
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function handle(Throwable $except, Response $response): Response
|
||||
{
|
||||
$data = [
|
||||
'code' => $except->getCode(),
|
||||
'error' => sprintf('(%s) %s', get_class($except), $except->getMessage()),
|
||||
'file' => sprintf('At %s line %d', $except->getFile(), $except->getLine())
|
||||
];
|
||||
return $response->withData($data);
|
||||
}
|
||||
}
|
||||
49
Swoft_BackEndApi/app/Exception/Handler/HttpExceptionHandler.php
Executable file
49
Swoft_BackEndApi/app/Exception/Handler/HttpExceptionHandler.php
Executable file
@@ -0,0 +1,49 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Exception\Handler;
|
||||
|
||||
use const APP_DEBUG;
|
||||
use function get_class;
|
||||
use ReflectionException;
|
||||
use function sprintf;
|
||||
use Swoft\Bean\Exception\ContainerException;
|
||||
use Swoft\Error\Annotation\Mapping\ExceptionHandler;
|
||||
use Swoft\Http\Message\Response;
|
||||
use Swoft\Http\Server\Exception\Handler\AbstractHttpErrorHandler;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Class HttpExceptionHandler
|
||||
*
|
||||
* @ExceptionHandler(\Throwable::class)
|
||||
*/
|
||||
class HttpExceptionHandler extends AbstractHttpErrorHandler
|
||||
{
|
||||
/**
|
||||
* @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) {
|
||||
return $response->withStatus(500)->withContent(
|
||||
sprintf(' %s At %s line %d', $e->getMessage(), $e->getFile(), $e->getLine())
|
||||
);
|
||||
}
|
||||
|
||||
$data = [
|
||||
'code' => $e->getCode(),
|
||||
'error' => sprintf('(%s) %s', get_class($e), $e->getMessage()),
|
||||
'file' => sprintf('At %s line %d', $e->getFile(), $e->getLine()),
|
||||
'trace' => $e->getTraceAsString(),
|
||||
];
|
||||
|
||||
// Debug is true
|
||||
return $response->withData($data);
|
||||
}
|
||||
}
|
||||
51
Swoft_BackEndApi/app/Exception/Handler/RpcExceptionHandler.php
Executable file
51
Swoft_BackEndApi/app/Exception/Handler/RpcExceptionHandler.php
Executable file
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user