gpt4 book ai didi

rest - ExceptionStrategy 不适用于 JsonStrategy

转载 作者:行者123 更新时间:2023-12-04 05:13:17 24 4
gpt4 key购买 nike

我有一个在这里报告的问题 (http://framework.zend.com/issues/browse/ZF2-379?page=com.atlassian.streams.streams-jira-plugin:activity-stream-issue-tab#issue-tabs)。有一个解决方案,但我不知道在哪里实现。任何人都可以帮忙吗?

问候,

最佳答案

这是 JsonExceptionStrategy 的代码 - 把它放在某个地方,比如在一个模块中。

namespace YourNamespace;

use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\Application;
use Zend\Mvc\View\Http\ExceptionStrategy;
use Zend\Mvc\MvcEvent;
use Zend\View\Model\JsonModel;

/**
*
* @since 1.0
* @author Tim Roediger <superdweebie@gmail.com>
*/
class JsonExceptionStrategy extends ExceptionStrategy
{

/**
* Attach the aggregate to the specified event manager
*
* @param EventManagerInterface $events
* @return void
*/
public function attach(EventManagerInterface $events)
{
$this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'prepareExceptionViewModel'), 100);
}

/**
* Create an exception json view model, and set the HTTP status code
*
* @todo dispatch.error does not halt dispatch unless a response is
* returned. As such, we likely need to trigger rendering as a low
* priority dispatch.error event (or goto a render event) to ensure
* rendering occurs, and that munging of view models occurs when
* expected.
* @param MvcEvent $e
* @return void
*/
public function prepareExceptionViewModel(MvcEvent $e)
{
// Do nothing if no error in the event
$error = $e->getError();
if (empty($error)) {
return;
}

// Do nothing if the result is a response object
$result = $e->getResult();
if ($result instanceof Response) {
return;
}

switch ($error) {
case Application::ERROR_CONTROLLER_NOT_FOUND:
case Application::ERROR_CONTROLLER_INVALID:
case Application::ERROR_ROUTER_NO_MATCH:
// Specifically not handling these
return;

case Application::ERROR_EXCEPTION:
default:
$exception = $e->getParam('exception');
$modelData = array(
'message' => $exception->getMessage(),
'type' => get_class($exception)
);

if ($this->displayExceptions()){
$modelData['exception'] = $exception;
}
$e->setResult(new JsonModel($modelData));
$e->setError(false);

$response = $e->getResponse();
if (!$response) {
$response = new HttpResponse();
$e->setResponse($response);
}
$response->setStatusCode(500);
break;
}
}
}

然后在 Module.php 中添加以下内容:
/**
*
* @param \Zend\EventManager\Event $event
*/
public function onBootstrap(MvcEvent $event)
{

$application = $event->getTarget();
$serviceManager = $application->getServiceManager();
$config = $serviceManager->get('Config');

// Config json enabled exceptionStrategy
$exceptionStrategy = new JsonExceptionStrategy();

$displayExceptions = false;

if (isset($config['view_manager']['display_exceptions'])) {
$displayExceptions = $config['view_manager']['display_exceptions'];
}

$exceptionStrategy->setDisplayExceptions($displayExceptions);
$exceptionStrategy->attach($application->getEventManager());
}

你可以走了!

关于rest - ExceptionStrategy 不适用于 JsonStrategy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14625360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com