29 lines
878 B
PHP
29 lines
878 B
PHP
<?php
|
|
/**
|
|
* Author: cfn <cfn@leapy.cn>
|
|
*/
|
|
|
|
namespace App\Exception\Handler;
|
|
|
|
use Hyperf\ExceptionHandler\ExceptionHandler;
|
|
use Hyperf\HttpMessage\Stream\SwooleStream;
|
|
use Swow\Psr7\Message\ResponsePlusInterface;
|
|
use Throwable;
|
|
|
|
class ServiceExceptionHandler extends ExceptionHandler
|
|
{
|
|
public function handle(Throwable $throwable, ResponsePlusInterface $response)
|
|
{
|
|
if (!$response->hasHeader('content-type')) {
|
|
$response = $response->withAddedHeader('content-type', 'application/json; charset=utf-8');
|
|
}
|
|
return $response->withHeader('Server', 'leapy')
|
|
->withStatus(200)
|
|
->withBody(new SwooleStream(json_encode(['code' => 1, 'msg' => $throwable->getMessage()])));
|
|
}
|
|
|
|
public function isValid(Throwable $throwable): bool
|
|
{
|
|
return $throwable instanceof \App\Exception\ServiceException;
|
|
}
|
|
} |