Returning an Error
rwsf::HttpServletResponse provides the function
sendError() for returning an error to the client. The function takes an HTTP status code, and an optional
std::string containing the payload of the error message. The code sample below sets the response to a Service Unavailable error, and suggests that the client try again in 10 minutes (600 seconds).
response.setIntHeader("Retry-After", 600);
response.sendError(rwsf::HttpServletResponse::SC_SERVICE_UNAVAILABLE);
Notice that the sample uses an
enum from class
rwsf::HttpServletResponse for the status code.
rwsf::HttpServletResponse provides values for the status codes defined by HTTP 1.1. See the
HydraExpress C++ API Reference Guide for details.
The Servlet Specification allows the servlet container to transmit the response to the client as soon as the servlet calls sendError(). The servlet should not change the response after calling sendError(), since the client may not receive the changes.
Errors returning from a servlet follow the standard error handling process. If the current context declares an error-page for the error code, the servlet container forwards the request to that resource. Otherwise, if the global web.xml file contains an error‑page for the error code, the container forwards the request to the resource declared in the global web.xml file. If neither web.xml file declares an error‑page, the container returns the response directly to the client.