/**
* 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));
}