From 7a7c954e2a8f685d5d4ce991ed12bbb440661c4a Mon Sep 17 00:00:00 2001 From: Simon Asika Date: Mon, 17 Jul 2017 00:39:49 +0800 Subject: [PATCH] Fix json error message --- .../Middleware/JsonApiMiddleware.php | 17 +++------- .../Middleware/JsonResponseMiddleware.php | 14 ++++----- src/Core/Error/ErrorManager.php | 31 +++++++++++++++++++ 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/Core/Controller/Middleware/JsonApiMiddleware.php b/src/Core/Controller/Middleware/JsonApiMiddleware.php index 4640cd86..8440ccb1 100644 --- a/src/Core/Controller/Middleware/JsonApiMiddleware.php +++ b/src/Core/Controller/Middleware/JsonApiMiddleware.php @@ -8,6 +8,7 @@ namespace Windwalker\Core\Controller\Middleware; +use Windwalker\Core\Error\ErrorManager; use Windwalker\Core\Response\Buffer\JsonBuffer; use Windwalker\Core\Utilities\Debug\BacktraceHelper; use Windwalker\Core\View\AbstractView; @@ -90,21 +91,13 @@ protected function handleException(\Exception $e) } } - $code = $e->getCode(); - - $code = ResponseHelper::validateStatus($code) ? $code : 500; - - $message = $e->getMessage(); - - if (Mbstring::isUtf8($message)) - { - $message = str_replace('%20', ' ', rawurlencode($message)); - } - $this->controller->setResponse( $this->controller ->getResponse() - ->withStatus($code, $message) + ->withStatus( + ErrorManager::normalizeCode($e->getCode()), + ErrorManager::normalizeMessage($e->getMessage()) + ) ); return new JsonBuffer($e->getMessage(), $data, false, $e->getCode()); diff --git a/src/Core/Controller/Middleware/JsonResponseMiddleware.php b/src/Core/Controller/Middleware/JsonResponseMiddleware.php index 53b34be7..5aafda55 100644 --- a/src/Core/Controller/Middleware/JsonResponseMiddleware.php +++ b/src/Core/Controller/Middleware/JsonResponseMiddleware.php @@ -8,7 +8,9 @@ namespace Windwalker\Core\Controller\Middleware; +use Windwalker\Core\Error\ErrorManager; use Windwalker\Debugger\Helper\DebuggerHelper; +use Windwalker\Http\Helper\ResponseHelper; use Windwalker\Http\Response\JsonResponse; use Windwalker\String\Mbstring; @@ -41,15 +43,11 @@ public function execute($data = null) ->addHandler(function ($exception) { /** @var $exception \Exception|\Throwable */ - $message = $exception->getMessage(); - - if (Mbstring::isUtf8($message)) - { - $message = str_replace('%20', ' ', rawurlencode($message)); - } - $data = ['error' => $exception->getMessage()]; - $response = (new JsonResponse($data))->withStatus($exception->getCode(), $message); + $response = (new JsonResponse($data))->withStatus( + ErrorManager::normalizeCode($exception->getCode()), + ErrorManager::normalizeMessage($exception->getMessage()) + ); $this->controller ->app diff --git a/src/Core/Error/ErrorManager.php b/src/Core/Error/ErrorManager.php index 5d62c932..b0318a43 100644 --- a/src/Core/Error/ErrorManager.php +++ b/src/Core/Error/ErrorManager.php @@ -9,7 +9,9 @@ namespace Windwalker\Core\Error; use Windwalker\Core\Application\WebApplication; +use Windwalker\Http\Helper\ResponseHelper; use Windwalker\Http\Response\HtmlResponse; +use Windwalker\String\Mbstring; /** * The ErrorManager class. @@ -394,4 +396,33 @@ public function setEngine($engine) return $this; } + + /** + * normalizeCode + * + * @param int $code + * + * @return int + */ + public static function normalizeCode($code) + { + return ResponseHelper::validateStatus($code) ? $code : 500; + } + + /** + * normalizeMessage + * + * @param string $message + * + * @return string + */ + public static function normalizeMessage($message) + { + if (Mbstring::isUtf8($message)) + { + $message = str_replace('%20', ' ', rawurlencode($message)); + } + + return trim(explode("\n", $message)[0]); + } }