Skip to content

Return the appropriate response code for REST errors. #94

Description

@MarcMichalsky

It is really annoying that the REST API always returns a 200 response code, even if, for example, an action is not allowed. This makes searching the logs and debugging more difficult.

/**
* generates a CiviCRM REST API compliant error
* and ends processing
*/
function civiproxy_rest_error($message) {
$error = array( 'is_error' => 1,
'error_message' => $message);
// TODO: Implement header();
print json_encode($error);
exit();
}

We should do something like this:

/**
 * generates a CiviCRM REST API compliant error
 * and ends processing
 */
function civiproxy_rest_error($message, $status_code = 500) {
  $error = [
    'is_error' => 1,
    'error_message' => $message,
  ];
  http_response_code($status_code); // Set the HTTP response code
  header('Content-Type: application/json'); // Send JSON response header
  echo json_encode($error);
  exit();
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions