You are here

public function RESTServer::handleException in Services 7.3

Same name and namespace in other branches
  1. 6.3 servers/rest_server/includes/RESTServer.inc \RESTServer::handleException()

Set proper header and message in case of exception.

Parameters

object $exception: Exception object

array $controller: Controller that was executed.

array $arguments: Set of arguments.

Return value

string $error_data Error message from exception.

1 call to RESTServer::handleException()
RESTServer::handle in servers/rest_server/includes/RESTServer.inc
Handles the call to the REST server

File

servers/rest_server/includes/RESTServer.inc, line 662
Class for handling REST calls.

Class

RESTServer
@file Class for handling REST calls.

Code

public function handleException($exception, $controller = array(), $arguments = array()) {
  $error_code = $exception
    ->getCode();
  $error_message = $exception
    ->getMessage();
  $error_data = method_exists($exception, 'getData') ? $exception
    ->getData() : '';
  switch ($error_code) {
    case 204:
      $error_header_status_message = $this
        ->formatHttpHeaderStatusMessage('204 No Content', $error_message);
      break;
    case 304:
      $error_header_status_message = $this
        ->formatHttpHeaderStatusMessage('304 Not Modified', $error_message);
      break;
    case 401:
      $error_header_status_message = $this
        ->formatHttpHeaderStatusMessage('401 Unauthorized', $error_message);
      break;
    case 404:
      $error_header_status_message = $this
        ->formatHttpHeaderStatusMessage('404 Not found', $error_message);
      break;
    case 406:
      $error_header_status_message = $this
        ->formatHttpHeaderStatusMessage('406 Not Acceptable', $error_message);
      break;
    case 200:
      $error_header_status_message = $this
        ->formatHttpHeaderStatusMessage('200', $error_message);
      break;
    case 201:
      $error_header_status_message = $this
        ->formatHttpHeaderStatusMessage('201', $error_message);
      break;
    default:
      if ($error_code >= 400 && $error_code < 600) {
        $error_header_status_message = $this
          ->formatHttpHeaderStatusMessage($error_code, $error_message);
      }
      else {
        watchdog_exception('Services', $exception);
        $error_header_status_message = $this
          ->formatHttpHeaderStatusMessage('500 Internal Server Error', "An error occurred ({$error_code}): {$error_message}");
      }
      break;
  }
  $error_alter_array = array(
    'code' => $error_code,
    'header_message' => &$error_header_status_message,
    'body_data' => &$error_data,
  );
  drupal_alter('rest_server_execute_errors', $error_alter_array, $controller, $arguments);
  drupal_add_http_header('Status', strip_tags(str_replace(array(
    "\r",
    "\n",
  ), '', $error_header_status_message)));
  return $error_data;
}