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

37 lines
780 B
PHP
Raw Normal View History

2021-08-04 15:17:25 +08:00
<?php
namespace Server;
2021-09-10 10:53:54 +08:00
use Protocol\Message\Stream;
2021-09-10 10:57:48 +08:00
use Server\Constrict\Response;
2021-09-10 10:53:54 +08:00
use Server\Constrict\ResponseInterface;
2021-09-10 10:57:48 +08:00
use Throwable;
2021-09-10 10:53:54 +08:00
2021-08-04 15:17:25 +08:00
/**
*
*/
class ExceptionHandlerDispatcher implements ExceptionHandlerInterface
{
2021-09-09 15:38:30 +08:00
/**
* @param Throwable $exception
2021-09-10 10:53:54 +08:00
* @param Response $response
2021-09-09 15:38:30 +08:00
* @return ResponseInterface
*/
2021-09-10 10:57:48 +08:00
public function emit(Throwable $exception, Response $response): ResponseInterface
{
$response->withContentType('text/html;charset=utf-8');
if ($exception->getCode() == 404) {
return $response->withBody(new Stream($exception->getMessage()))
->withStatus(404);
}
$code = $exception->getCode() == 0 ? 500 : $exception->getCode();
return $response->withBody(new Stream(jTraceEx($exception, null, true)))
->withStatus($code);
}
2021-08-04 15:17:25 +08:00
}