You are here

function _restful_build_http_api_error in RESTful 7.2

Helper function to build the structured array for the error output.

Parameters

RestfulException $exception: The exception.

ResponseInterface $response: The response object to alter.

Return value

array The structured output.

4 calls to _restful_build_http_api_error()
restful_delivery in ./restful.module
Returns data in JSON format.
restful_menu_access_callback in ./restful.module
Access callback; Determine access for an API call.
restful_menu_process_callback in ./restful.module
Page callback; Return the response for an API call.
_restful_get_data_from_menu_status in ./restful.module
Convert a menu status response to a valid JSON.

File

./restful.module, line 421

Code

function _restful_build_http_api_error(RestfulException $exception, ResponseInterface $response = NULL) {
  $response = $response ?: restful()
    ->getResponse();

  // Adhere to the API Problem draft proposal.
  $exception
    ->setHeader('Content-Type', 'application/problem+json; charset=utf-8');
  $result = array(
    'type' => $exception
      ->getType(),
    'title' => $exception
      ->getMessage(),
    'status' => $exception
      ->getCode(),
    'detail' => $exception
      ->getDescription(),
  );
  if ($instance = $exception
    ->getInstance()) {
    $result['instance'] = $instance;
  }
  if ($errors = $exception
    ->getFieldErrors()) {
    $result['errors'] = $errors;
  }
  $headers = $response
    ->getHeaders();
  foreach ($exception
    ->getHeaders() as $header_name => $header_value) {
    $headers
      ->add(HttpHeader::create($header_name, $header_value));
  }
  drupal_page_is_cacheable(FALSE);

  // Add a log entry with the error / warning.
  if ($exception
    ->getCode() < 500) {

    // Even though it's an exception, it's in fact not a server error - it
    // might be just access denied, or a bad request, so we just want to log
    // it, but without marking it as an actual exception.
    watchdog('restful', $exception
      ->getMessage());
  }
  else {
    watchdog_exception('restful', $exception);
  }
  return $result;
}