public function render() { $params = []; $errorCode = ApiError::UNKNOWN; if ($this->exception instanceof LackParametersException) { $errorCode = ApiError::LACK_PARAMETERS; $params['lackParameters'] = explode(', ', $this->exception->getMessage()); } elseif ($this->exception instanceof UnauthenticatedException) { $errorCode = ApiError::UNAUTHENTICATED; } elseif ($this->exception instanceof UnauthorizedException) { $errorCode = ApiError::NOT_AUTHENTICATED; } $statusCode = $this->response->statusCode(); $errorMessages = ApiError::messages(); if (isset($errorMessages[$statusCode])) { $errorCode = $statusCode; } if (Configure::read('debug') > 1 && $errorCode == ApiError::UNKNOWN) { $params['debug'] = ['message' => $this->exception->getMessage(), 'trace' => explode("\n", $this->exception->getTraceAsString())]; } $params = ['success' => false, 'code' => $statusCode, 'errorCode' => $errorCode, 'errorMessage' => ApiError::message($errorCode)] + $params; $this->response->body(json_encode($params)); $this->response->type('json'); $this->response->send(); }
/** * レスポンスを失敗状態にします。 * エラーコード、HTTPステータスを指定できます。 * * @param string $errorCode * @param mixed $httpStatus * @return void * @see ApiError */ public function failure($errorCode, $httpStatus = 500) { $this->setResponse(['success' => false, 'code' => $httpStatus, 'errorCode' => $errorCode, 'errorMessage' => ApiError::message($errorCode)]); }
/** * test failure() method * * @test */ public function failure() { $this->generateComponent(); $this->Api->failure(ApiError::VALIDATION_ERROR); $this->assertSame(['success' => false, 'code' => 500, 'errorCode' => ApiError::VALIDATION_ERROR, 'errorMessage' => ApiError::message(ApiError::VALIDATION_ERROR)], $this->Api->getResponse()); $this->generateComponent(); $this->Api->failure(ApiError::VALIDATION_ERROR, 501); $this->assertSame(501, $this->Api->getResponse()['code']); }