This commit is contained in:
2021-09-10 10:57:48 +08:00
parent abb9508c36
commit 79e5907765
2 changed files with 22 additions and 28 deletions
+9 -13
View File
@@ -16,10 +16,7 @@ class Response implements ResponseInterface
use Message; use Message;
const CONTENT_TYPE_JSON = 'application/json;charset=utf-8';
const CONTENT_TYPE_HTML = 'text/html;charset=utf-8'; const CONTENT_TYPE_HTML = 'text/html;charset=utf-8';
const CONTENT_TYPE_STREAM = 'octet-stream';
const CONTENT_TYPE_XML = 'application/xml;charset=utf-8';
protected int $statusCode = 200; protected int $statusCode = 200;
@@ -145,23 +142,23 @@ class Response implements ResponseInterface
/** /**
* @param $data * @param $data
* @param string $contentType
* @return static * @return static
* @throws Exception
*/ */
public function json($data): static public function json($data, string $contentType = 'application/json;charset=utf-8'): static
{ {
$this->stream->write(json_encode($data)); $this->stream->write(json_encode($data));
return $this->withContentType(self::CONTENT_TYPE_JSON); return $this->withContentType($contentType);
} }
/** /**
* @param $data * @param $data
* @param string $contentType
* @return static * @return static
* @throws Exception
*/ */
public function html($data): static public function html($data, string $contentType = 'text/html;charset=utf-8'): static
{ {
if (!is_string($data)) { if (!is_string($data)) {
$data = json_encode($data); $data = json_encode($data);
@@ -169,21 +166,20 @@ class Response implements ResponseInterface
$this->stream->write((string)$data); $this->stream->write((string)$data);
return $this->withContentType(self::CONTENT_TYPE_HTML); return $this->withContentType($contentType);
} }
/** /**
* @param $data * @param $data
* @param string $contentType
* @return static * @return static
* @throws Exception
*/ */
public function xml($data): static public function xml($data, string $contentType = 'application/xml;charset=utf-8'): static
{ {
$this->stream->write(Help::toXml($data)); $this->stream->write(Help::toXml($data));
return $this->withContentType(self::CONTENT_TYPE_XML); return $this->withContentType($contentType);
} }
+13 -15
View File
@@ -3,11 +3,10 @@
namespace Server; namespace Server;
use Server\Constrict\Response;
use Protocol\Message\Response as CResponse;
use Throwable;
use Protocol\Message\Stream; use Protocol\Message\Stream;
use Server\Constrict\Response;
use Server\Constrict\ResponseInterface; use Server\Constrict\ResponseInterface;
use Throwable;
/** /**
@@ -22,17 +21,16 @@ class ExceptionHandlerDispatcher implements ExceptionHandlerInterface
* @param Response $response * @param Response $response
* @return ResponseInterface * @return ResponseInterface
*/ */
public function emit(Throwable $exception, Response $response): ResponseInterface public function emit(Throwable $exception, Response $response): ResponseInterface
{ {
if ($exception->getCode() == 404) { $response->withContentType('text/html;charset=utf-8');
return $response->withBody(new Stream($exception->getMessage())) if ($exception->getCode() == 404) {
->withContentType(CResponse::CONTENT_TYPE_HTML) return $response->withBody(new Stream($exception->getMessage()))
->withStatus(404); ->withStatus(404);
} }
$code = $exception->getCode() == 0 ? 500 : $exception->getCode(); $code = $exception->getCode() == 0 ? 500 : $exception->getCode();
return $response->withBody(new Stream(jTraceEx($exception, null, true))) return $response->withBody(new Stream(jTraceEx($exception, null, true)))
->withContentType(CResponse::CONTENT_TYPE_HTML) ->withStatus($code);
->withStatus($code); }
}
} }