Send a JSON HTTP 400 response
public respondWithError ( string $msg = '', integer $status = 400 ) : Symfony\Component\HttpFoundation\JsonResponse | ||
$msg | string | The error message (not displayed to the user) |
$status | integer | The HTTP status code |
리턴 | Symfony\Component\HttpFoundation\JsonResponse |
/** * Send error HTTP response * * @param string $error Error message * @param int $status_code HTTP status code * @param array $headers HTTP headers (will be discarded on AJAX requests) * @return Response * @throws InvalidParameterException */ public function respondWithError($error, $status_code = ELGG_HTTP_BAD_REQUEST, array $headers = []) { if ($this->ajax->isReady()) { return $this->send($this->ajax->respondWithError($error, $status_code)); } if ($this->isXhr()) { // xhr calls to non-actions (e.g. ajax/view or ajax/form) need to receive proper HTTP status code return $this->send($this->prepareResponse($error, $status_code, $headers)); } $forward_url = $this->request->headers->get('Referer'); if (!$this->isAction()) { $params = ['current_url' => current_page_url(), 'forward_url' => $forward_url]; // For BC, let plugins serve their own error page // @see elgg_error_page_handler $forward_reason = (string) $status_code; $forward_url = $this->hooks->trigger('forward', $forward_reason, $params, $forward_url); if ($this->response_sent) { // Response was sent from a forward hook return $this->response_sent; } $params['type'] = $forward_reason; $error_page = elgg_view_resource('error', $params); return $this->send($this->prepareResponse($error_page, $status_code)); } $forward_url = elgg_normalize_url($forward_url); return $this->send($this->prepareRedirectResponse($forward_url)); }
public function testCanNotSendANewResponseAfterAjaxErrorResponseIsSent() { $service = $this->createService(); ob_start(); $json_response = $this->ajax->respondWithError('error'); ob_get_clean(); $this->assertEquals($json_response, $service->send($service->prepareResponse('bar'))); }