Skip to content

Commit

Permalink
Fix json error message
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Jul 16, 2017
1 parent 3e6b424 commit 7a7c954
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
17 changes: 5 additions & 12 deletions src/Core/Controller/Middleware/JsonApiMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
14 changes: 6 additions & 8 deletions src/Core/Controller/Middleware/JsonResponseMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions src/Core/Error/ErrorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]);
}
}

0 comments on commit 7a7c954

Please sign in to comment.