/** * Handles exception responses * * @param Exception $exception */ protected function handleException(Exception $exception) { $GLOBALS['logic_hook']->call_custom_logic('', "handle_exception", $exception); if (is_a($exception, "SugarApiException")) { $httpError = $exception->getHttpCode(); $errorLabel = $exception->getErrorLabel(); $message = $exception->getMessage(); } elseif (is_a($exception, "OAuth2ServerException")) { //The OAuth2 Server uses a slightly different exception API $httpError = $exception->getHttpCode(); $errorLabel = $exception->getMessage(); $message = $exception->getDescription(); } else { $httpError = 500; $errorLabel = 'unknown_error'; $message = $exception->getMessage(); } if (!empty($exception->extraData)) { $data = $exception->extraData; } $this->response->setStatus($httpError); $GLOBALS['log']->error('An exception happened: ( ' . $httpError . ': ' . $errorLabel . ')' . $message); // For edge cases when an HTML response is needed as a wrapper to JSON if (isset($_REQUEST['format']) && $_REQUEST['format'] == 'sugar-html-json') { $this->response->setType(RestResponse::JSON_HTML, true); } else { // Send proper headers $this->response->setType(RestResponse::JSON, true); } $this->response->setHeader("Cache-Control", "no-store"); $replyData = array('error' => $errorLabel); if ($errorLabel === 'metadata_out_of_date') { $mM = $this->getMetadataManager(); // In case of a `metadata_out_of_date` error, return the current // valid metadata hash so the client knows if it is worth // re-syncing. $replyData['metadata_hash'] = $mM->getMetadataHash(); $replyData['user_hash'] = $this->user->getUserMDHash(); } if (!empty($message)) { $replyData['error_message'] = $message; } if (!empty($data)) { $replyData = array_merge($replyData, $data); } $this->response->setContent($replyData); }