Files
kiri-core/http-server/ExceptionHandlerDispatcher.php
T

37 lines
941 B
PHP
Raw Normal View History

2021-08-04 15:17:25 +08:00
<?php
namespace Server;
use Server\Constrict\Response;
use Server\Constrict\Response as CResponse;
2021-08-28 01:21:23 +08:00
use Server\Message\Stream;
2021-08-04 15:39:06 +08:00
use Throwable;
2021-08-04 15:17:25 +08:00
/**
*
*/
class ExceptionHandlerDispatcher implements ExceptionHandlerInterface
{
2021-08-28 01:21:23 +08:00
/**
* @param Throwable $exception
* @param CResponse $response
* @return ResponseInterface
*/
public function emit(Throwable $exception, Response $response): ResponseInterface
{
if ($exception->getCode() == 404) {
return $response->withBody(new Stream($exception->getMessage()))
2021-08-28 01:37:47 +08:00
->withContentType(Message\Response::CONTENT_TYPE_HTML)
2021-08-28 01:21:23 +08:00
->withStatus(404);
}
$code = $exception->getCode() == 0 ? 500 : $exception->getCode();
return $response->withBody(new Stream(jTraceEx($exception, null, true)))
2021-08-28 01:37:47 +08:00
->withContentType(Message\Response::CONTENT_TYPE_HTML)
2021-08-28 01:21:23 +08:00
->withStatus($code);
}
2021-08-04 15:17:25 +08:00
}