acceptedMimeTypes; } /** * @OA\Schema( * schema="tao.RestTrait.FailureResponse", * description="Error response with success=false", * allOf={ * @OA\Schema(ref="#/components/schemas/tao.RestTrait.BaseResponse") * }, * @OA\Property( * property="success", * type="boolean", * example=false, * description="Indicates error" * ), * @OA\Property( * property="errorCode", * type="integer", * description="Exception error code" * ), * @OA\Property( * property="errorMsg", * type="string", * description="Exception message, not localized" * ) * ) * * Return failed Rest response * Set header http by using handle() * If $withMessage is true: * Send response with success, code, message & version of TAO * * @param Exception $exception * @param $withMessage * @throws common_exception_NotImplemented */ protected function returnFailure(Exception $exception, $withMessage = true) { $handler = new RestExceptionHandler(); $handler->sendHeader($exception); $data = []; if ($withMessage) { $data['success'] = false; $data['errorCode'] = $exception->getCode(); $data['errorMsg'] = $this->getErrorMessage($exception); $data['version'] = TAO_VERSION; } echo $this->encode($data); exit(0); } /** * @OA\Schema( * schema="tao.RestTrait.SuccessResponse", * description="Response with data and success=true", * allOf={ * @OA\Schema(ref="#/components/schemas/tao.RestTrait.BaseResponse"), * }, * @OA\Property( * property="success", * type="boolean", * example=true, * description="Indicates success" * ), * @OA\Property( * property="data", * type="object", * description="Payload" * ) * ) * * Return success Rest response * Send response with success, data & version of TAO * * @param array $rawData * @param bool $withMessage * @throws common_exception_NotImplemented */ protected function returnSuccess($rawData = [], $withMessage = true) { $data = []; if ($withMessage) { $data['success'] = true; $data['data'] = $rawData; $data['version'] = TAO_VERSION; } else { $data = $rawData; } echo $this->encode($data); exit(0); } /** * Encode data regarding responseEncoding * * @param $data * @return string * @throws common_exception_NotImplemented */ protected function encode($data) { switch ($this->responseEncoding) { case "application/rdf+xml": throw new common_exception_NotImplemented(); break; case "text/xml": case "application/xml": return tao_helpers_Xml::from_array($data); case "application/json": default: return json_encode($data); } } /** * Generate safe message preventing exposing sensitive date in non develop mode * @param Exception $exception * @return string */ protected function getErrorMessage(Exception $exception) { $defaultMessage = __('Unexpected error. Please contact administrator'); if (DEBUG_MODE) { $defaultMessage = $exception->getMessage(); } return ($exception instanceof common_exception_UserReadableException) ? $exception->getUserMessage() : $defaultMessage; } }