Files
kiri-router/src/Base/ExceptionHandlerDispatcher.php
T

35 lines
793 B
PHP
Raw Normal View History

2023-04-15 23:29:27 +08:00
<?php
namespace Kiri\Message;
use Kiri\Message\Constrict\Response;
use Kiri\Message\Constrict\ResponseInterface;
use Kiri\Message\Abstracts\ExceptionHandlerInterface;
use Throwable;
/**
*
*/
class ExceptionHandlerDispatcher implements ExceptionHandlerInterface
{
/**
* @param Throwable $exception
* @param Response $response
* @return ResponseInterface
*/
public function emit(Throwable $exception, Response $response): ResponseInterface
{
$response->withContentType(ContentType::HTML);
if ($exception->getCode() == 404) {
return $response->withBody(new Stream($exception->getMessage()))->withStatus(404);
}
return $response->withBody(new Stream(jTraceEx($exception, null, true)))
->withStatus($exception->getCode() == 0 ? 500 : $exception->getCode());
}
}