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

37 lines
797 B
PHP
Raw Normal View History

2023-04-15 23:29:27 +08:00
<?php
2023-04-16 01:24:30 +08:00
declare(strict_types=1);
2023-04-15 23:29:27 +08:00
2023-04-15 23:31:16 +08:00
namespace Kiri\Router\Base;
2023-04-15 23:29:27 +08:00
2023-04-15 23:31:16 +08:00
use Kiri\Router\ContentType;
use Kiri\Router\Interface\ExceptionHandlerInterface;
use Psr\Http\Message\ResponseInterface;
2023-04-15 23:29:27 +08:00
use Throwable;
2023-04-15 23:31:16 +08:00
use Kiri\Router\Constrict\Stream;
2023-04-15 23:29:27 +08:00
/**
*
*/
class ExceptionHandlerDispatcher implements ExceptionHandlerInterface
{
/**
* @param Throwable $exception
2023-04-15 23:31:16 +08:00
* @param object $response
2023-04-15 23:29:27 +08:00
* @return ResponseInterface
*/
2023-04-15 23:31:16 +08:00
public function emit(Throwable $exception, object $response): ResponseInterface
2023-04-15 23:29:27 +08:00
{
2023-04-15 23:31:16 +08:00
$response->withContentType(ContentType::HTML)->withBody(new Stream(jTraceEx($exception, null, true)));
2023-04-15 23:29:27 +08:00
if ($exception->getCode() == 404) {
2023-04-15 23:31:16 +08:00
return $response->withStatus(404);
} else {
return $response->withStatus($exception->getCode() == 0 ? 500 : $exception->getCode());
2023-04-15 23:29:27 +08:00
}
}
}