You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1013 B
37 lines
1013 B
<?php
|
|
|
|
namespace App\Exception\Handler;
|
|
|
|
use App\Exception\SsdbException;
|
|
use Hyperf\ExceptionHandler\ExceptionHandler;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Throwable;
|
|
use App\Constants\ErrorCode;
|
|
use Hyperf\HttpMessage\Stream\SwooleStream;
|
|
|
|
class SsdbExceptionHandler extends ExceptionHandler
|
|
{
|
|
|
|
public function handle(Throwable $throwable, ResponseInterface $response)
|
|
{
|
|
$this->stopPropagation();
|
|
|
|
$content = json_encode(
|
|
[
|
|
"status" => 'error',
|
|
"code" => ErrorCode::SSDB_ERROR,
|
|
"result" => [],
|
|
"message" => $throwable->getMessage() ?: ErrorCode::getMessage(ErrorCode::SSDB_ERROR)
|
|
]
|
|
);
|
|
|
|
return $response->withHeader('Content-Type', 'application/json')
|
|
->withStatus($throwable->status)
|
|
->withBody(new SwooleStream($content));
|
|
}
|
|
|
|
public function isValid(Throwable $throwable): bool
|
|
{
|
|
return $throwable instanceof SsdbException;
|
|
}
|
|
}
|