Class HTTPErrorHandler
This class provides a centralized mechanism for handling errors in HTTP request processing by generating and sending appropriate HTTP error responses to clients. It supports standard HTTP error codes and provides exception-to-error-code mapping for common error scenarios.
Key features:
- Standardized HTTP error response generation with proper headers
- Automatic exception-to-HTTP-status-code mapping
- Integration with
ServerLoggerfor error logging - HTML-formatted error messages for browser-friendly display
Supported HTTP error codes:
- 400 Bad Request - Malformed or invalid client requests
- 404 Not Found - Requested resource does not exist
- 405 Method Not Allowed - HTTP method not supported for the resource
- 500 Internal Server Error - Unexpected server-side errors
- 501 Not Implemented - Requested functionality not yet implemented
All error responses follow the HTTP/1.1 specification with:
- Proper status line format
- Content-Type header set to text/html with UTF-8 encoding
- Connection: close header to indicate non-persistent connections
- Simple HTML body with error status code and message
Thread-safety: This class is stateless and all methods are static, making it inherently thread-safe for concurrent use across multiple request handlers.
Example usage:
try {
// Process request
} catch (NotFoundException e) {
HTTPErrorHandler.handleException(socket, e);
}
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidhandleException(Socket socket, Exception e) Handles an exception by mapping it to the appropriate HTTP error response.static voidsendBadRequest(Socket socket) Sends a 400 Bad Request HTTP error response.static voidsendInternalError(Socket socket) Sends a 500 Internal Server Error HTTP error response.static voidsendMethodNotAllowed(Socket socket) Sends a 405 Method Not Allowed HTTP error response.static voidsendNotFound(Socket socket) Sends a 404 Not Found HTTP error response.static voidsendNotImplemented(Socket socket) Sends a 501 Not Implemented HTTP error response.
-
Constructor Details
-
HTTPErrorHandler
public HTTPErrorHandler()
-
-
Method Details
-
sendBadRequest
Sends a 400 Bad Request HTTP error response.This error indicates that the server cannot process the request due to client error, such as malformed request syntax, invalid request message framing, or deceptive request routing.
Common scenarios for 400 errors:
- Malformed HTTP headers
- Invalid request line format
- Missing required headers
- Invalid request body format
- Parameters:
socket- The client socket to which the error response will be sent.
-
sendNotFound
Sends a 404 Not Found HTTP error response.This error indicates that the server cannot find the requested resource. The resource may have been deleted, moved, or never existed. This is one of the most common HTTP error codes.
Common scenarios for 404 errors:
- Requested file does not exist in the document root
- Invalid URL path specified by the client
- Resource has been deleted or moved
- Route is not registered in the server configuration
- Parameters:
socket- The client socket to which the error response will be sent.
-
sendInternalError
Sends a 500 Internal Server Error HTTP error response.This error indicates that the server encountered an unexpected condition that prevented it from fulfilling the request. This is a generic error used when no more specific error code is appropriate.
Common scenarios for 500 errors:
- Unhandled exceptions in request handlers
- Database connection failures
- File I/O errors (permission denied, disk full)
- Configuration errors
- Programming errors (NullPointerException, etc.)
- Parameters:
socket- The client socket to which the error response will be sent.
-
sendMethodNotAllowed
Sends a 405 Method Not Allowed HTTP error response.This error indicates that the HTTP method used in the request is not supported for the requested resource. For example, attempting a POST request on a resource that only supports GET.
Common scenarios for 405 errors:
- Using POST on a read-only resource
- Using DELETE on a resource that doesn't support deletion
- Request handler not implementing the requested HTTP method
- Parameters:
socket- The client socket to which the error response will be sent.
-
sendNotImplemented
Sends a 501 Not Implemented HTTP error response.This error indicates that the server does not support the functionality required to fulfill the request. Unlike 405 Method Not Allowed, which indicates the method is recognized but not allowed for the resource, 501 indicates the server does not recognize or support the method at all.
Common scenarios for 501 errors:
- HTTP methods not yet implemented (e.g., TRACE, CONNECT)
- Features under development
- Unsupported protocol versions
- Parameters:
socket- The client socket to which the error response will be sent.
-
handleException
Handles an exception by mapping it to the appropriate HTTP error response.This method provides centralized exception handling for HTTP request processing. It examines the type of exception and dispatches to the appropriate error-sending method. All exceptions are logged via
ServerLoggerfor debugging and monitoring purposes.Exception-to-status-code mapping:
BadRequestException→ 400 Bad RequestNotFoundException→ 404 Not FoundNotImplementedException→ 501 Not ImplementedInternalServerErrorException→ 500 Internal Server Error- All other exceptions → 500 Internal Server Error (fallback)
After sending the error response, the exception message is logged at ERROR level to both server and client destinations for comprehensive error tracking.
- Parameters:
socket- The client socket to which the error response will be sent.e- The exception to handle. Must not be null.
-