64 $this->context->getRequest()->decodeJson();
74 private function getSourceParametersList()
76 if (!$this->context->getServer()->get(
'HTTP_BX_AJAX_QB'))
79 $this->context->getRequest()->getPostList(),
80 $this->context->getRequest()->getQueryList(),
85 Web\Json::decode($this->context->getRequest()->getPost(
'bx_data'))
98 $router =
new Router($this->context->getRequest());
102 [$controller, $actionName] =
$router->getControllerAndAction();
105 throw new SystemException(
'Could not find controller for the request', self::EXCEPTION_UNKNOWN_CONTROLLER);
110 catch (\Throwable $e)
112 $errorCollection =
new ErrorCollection();
114 $this->processRunError($e, $errorCollection);
115 $this->finalizeControllerResult($controller ??
null,
null, $errorCollection);
131 if (is_string($controller))
133 $controller = ControllerBuilder::build($controller, [
134 'scope' => Controller::SCOPE_AJAX,
135 'currentUser' => CurrentUser::get(),
139 $this->registerAutoWirings();
141 $result = $controller->run(
$action, $this->getSourceParametersList());
142 $errorCollection->add($controller->getErrors());
144 catch (\Throwable $e)
146 $this->processRunError($e, $errorCollection);
150 $this->finalizeControllerResult($controller,
$result, $errorCollection);
166 $controller->finalizeResponse(
$response);
175 private function shouldWriteToLogException(\Throwable $e): bool
177 if ($e instanceof AutoWire\BinderArgumentException)
182 if ($e instanceof BadSignatureException)
187 $unnecessaryCodes = [
188 self::EXCEPTION_UNKNOWN_CONTROLLER,
189 Router::EXCEPTION_INVALID_COMPONENT_INTERFACE,
190 Router::EXCEPTION_INVALID_COMPONENT,
191 Router::EXCEPTION_INVALID_AJAX_MODE,
192 Router::EXCEPTION_NO_MODULE,
193 Router::EXCEPTION_INVALID_MODULE_NAME,
194 Router::EXCEPTION_INVALID_COMPONENT_NAME,
195 Router::EXCEPTION_NO_COMPONENT,
196 Router::EXCEPTION_NO_COMPONENT_AJAX_CLASS,
199 if ($e instanceof SystemException)
201 if (\in_array($e->getCode(), $unnecessaryCodes,
true))
206 if ($this->isDebugMode() && ($e->getCode() === Router::EXCEPTION_NO_CONFIGURATION))
215 private function isDebugMode(): bool
217 $exceptionHandling = Configuration::getValue(
'exception_handling');
219 return !empty($exceptionHandling[
'debug']);
222 private function processRunError(\Throwable $e, ErrorCollection $errorCollection): void
224 $errorCollection[] =
new Error($e->getMessage(), $e->getCode());
226 $debugMode = $this->isDebugMode();
229 $errorCollection[] =
new Error(Diag\ExceptionHandlerFormatter::format($e));
230 if ($e->getPrevious())
232 $errorCollection[] =
new Error(Diag\ExceptionHandlerFormatter::format($e->getPrevious()));
236 if ($debugMode || $this->shouldWriteToLogException($e))
242 private function registerAutoWirings()
244 AutoWire\Binder::registerGlobalAutoWiredParameter(
new AutoWire\Parameter(
245 PageNavigation::class,
247 $pageNavigation =
new PageNavigation(
'nav');
249 ->setPageSizes(range(1, 50))
253 return $pageNavigation;
257 AutoWire\Binder::registerGlobalAutoWiredParameter(
new AutoWire\Parameter(
260 return new JsonPayload();
264 AutoWire\Binder::registerGlobalAutoWiredParameter(
new AutoWire\Parameter(
267 return CurrentUser::get();
281 private function buildResponse($actionResult, ErrorCollection $errorCollection)
283 if ($actionResult instanceof HttpResponse)
285 return $actionResult;
288 if (!$errorCollection->isEmpty())
294 AjaxJson::STATUS_ERROR,
299 return new AjaxJson($actionResult);
setContext(Context $context)
end($status=0, Response $response=null)
runController($controller, $action)