first commit
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user