
EventHttpRequest::sendError는 HTTP 요청을 처리하는 중에 오류를 발생시키는 메서드입니다. 이 메서드를 사용하여 오류를 발생시키면, 지정된 HTTP 상태 코드와 오류 메시지를 클라이언트에게 반환합니다.
HTTP 상태 코드는 클라이언트에게 오류의 종류와 이유를 알려주기 위해 사용됩니다. 예를 들어, 400 Bad Request는 클라이언트가 잘못된 요청을 보낸 경우에 발생하고, 500 Internal Server Error는 서버 내부에서 오류가 발생한 경우에 발생합니다.
EventHttpRequest::sendError를 호출할 때, 사용할 수 있는 HTTP 상태 코드는 다음과 같습니다.
- 400 Bad Request : 클라이언트가 잘못된 요청을 보낸 경우
- 401 Unauthorized : 인증이 실패한 경우
- 403 Forbidden : 클라이언트가 요청한 리소스에 접근할 권리가 없는 경우
- 404 Not Found : 요청한 리소스가 서버에 존재하지 않는 경우
- 500 Internal Server Error : 서버 내부에서 오류가 발생한 경우
에러 코드를 확인하고 처리하는 방법은 다음과 같습니다.
1. 에러 코드를 확인하기 위해 EventHttpRequest의 getStatusCode() 메서드를 사용할 수 있습니다.
2. 에러 코드에 따라 적절한 오류 메시지를 반환할 수 있습니다.
3. 에러 코드를 처리하기 위해 try-catch 블록을 사용할 수 있습니다.
EventHttpRequest::sendError를 사용하여 에러를 발생시키고 처리하는 방법에 대한 예제는 다음과 같습니다.
#hostingforum.kr
php
class EventHttpRequest extends HTTPRequest {
public function sendError($statusCode, $message) {
$this->statusCode = $statusCode;
$this->statusMessage = $message;
return $this;
}
}
// 예제
$httpRequest = new EventHttpRequest();
$httpRequest->sendError(400, 'Bad Request');
echo $httpRequest->getStatusCode(); // 400
echo $httpRequest->getStatusMessage(); // Bad Request
try {
// 오류를 발생시키는 코드
throw new Exception('오류 메시지');
} catch (Exception $e) {
// 오류를 처리하는 코드
$httpRequest->sendError(500, 'Internal Server Error');
echo $httpRequest->getStatusCode(); // 500
echo $httpRequest->getStatusMessage(); // Internal Server Error
}
이 예제에서는 EventHttpRequest::sendError를 사용하여 400 Bad Request와 500 Internal Server Error를 발생시키고, 에러 코드를 확인하고 처리하는 방법을 보여줍니다.
2025-06-11 09:23