Skip to content
Download

HttpException

mach::HttpException

Represents an HTTP exception with an associated status code and error message.

HttpException is the base class for HTTP exceptions in Mach. Throwing an HttpException during request processing terminates the current request and produces an HTTP error response with the exception’s status code and message.

Use HttpException directly when a specific status code is required, or use one of Mach’s derived exception types for common HTTP errors.

HttpException derives from std::runtime_error.

Primary header:

#include <mach/exceptions/HttpException.hpp>

Convenience header:

#include <mach/exceptions.hpp>
class HttpException : public std::runtime_error {
public:
HttpException(http::StatusCode status, std::string message);
[[nodiscard]]
http::StatusCode status() const noexcept;
};
Name Description
status() Returns the HTTP status code associated with the exception.
what() Returns the exception message inherited from std::runtime_error.

HttpException is immutable after construction and may be safely accessed from multiple threads.

The exception owns its error message and associated HTTP status code.

For common HTTP errors, prefer the corresponding derived exception type when one is available.

throw mach::HttpException(
mach::http::StatusCode::BadRequest,
"The request is invalid."
);
Name Description
BadRequestException Represents an HTTP 400 Bad Request.
UnauthorizedException Represents an HTTP 401 Unauthorized.
ForbiddenException Represents an HTTP 403 Forbidden.
NotFoundException Represents an HTTP 404 Not Found.
ConflictException Represents an HTTP 409 Conflict.