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

38 lines
820 B
PHP
Raw Normal View History

2021-08-04 15:17:25 +08:00
<?php
namespace Server;
2021-09-21 02:46:21 +08:00
use Http\Message\ContentType;
2021-09-16 14:53:36 +08:00
use Http\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
{
2021-09-21 02:46:21 +08:00
$response->withContentType(ContentType::HTML)->withCharset('utf-8');
2021-09-10 10:57:48 +08:00
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
}