The generic base class for handler exceptions. Thrown exceptions should typically derive from ExceptionWithCode instead.
More...
#include <userver/server/handlers/exceptions.hpp>
|
template<typename... Args> |
| CustomHandlerException (HandlerErrorCode handler_code, Args &&... args) |
| Construct manually from a set of (mostly optional) arguments, which describe the error details.
|
|
| CustomHandlerException (HandlerErrorCode handler_code) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
|
| CustomHandlerException (ServiceErrorCode service_code, ExternalBody external_body, InternalMessage internal_message, HandlerErrorCode handler_code, ExtraHeaders headers={}, formats::json::Value details={}) |
|
template<typename MessageBuilder , typename = std::enable_if_t<impl::kIsMessageBuilder<MessageBuilder>>> |
| CustomHandlerException (MessageBuilder &&builder, HandlerErrorCode handler_code) |
|
HandlerErrorCode | GetCode () const |
|
const std::string & | GetServiceCode () const |
|
bool | IsExternalErrorBodyFormatted () const |
|
const std::string & | GetExternalErrorBody () const |
|
const formats::json::Value & | GetDetails () const |
|
const Headers & | GetExtraHeaders () const |
|
The generic base class for handler exceptions. Thrown exceptions should typically derive from ExceptionWithCode instead.
Definition at line 210 of file exceptions.hpp.
◆ ExternalBody
◆ ExtraHeaders
◆ HandlerErrorCode
◆ InternalMessage
◆ ServiceErrorCode
◆ CustomHandlerException() [1/4]
template<typename... Args>
server::handlers::CustomHandlerException::CustomHandlerException |
( |
HandlerErrorCode |
handler_code, |
|
|
Args &&... |
args |
|
) |
| |
|
inline |
Construct manually from a set of (mostly optional) arguments, which describe the error details.
Allowed arguments
Example:
"repetitions (42, we only allow 10)"},
server::http::HttpStatus::kBadRequest);
EXPECT_THAT(exc.GetExternalErrorBody(),
testing::HasSubstr("Failed to post: spam detected"));
EXPECT_THAT(exc.what(), testing::HasSubstr("42, we only allow 10"));
}
Message builders
A message builder is a class that satisfies the following requirements:
- provides a
GetExternalBody() const
function to form an external body
- has an optional
kIsExternalBodyFormatted
set to true to forbid changing the external body
- has an optional
GetServiceCode() const
function to return machine readable error code
- has an optional
GetInternalMessage() const
function to form an message for logging an error
Some message builder data can be overridden by explicitly passed args, if these args go after the message builder.
Example:
class CustomErrorBuilder {
public:
static constexpr bool kIsExternalBodyFormatted = true;
CustomErrorBuilder(std::string status, std::string msg) {
error["code"] = std::move(status);
error["message"] = std::move(msg);
}
std::string GetExternalBody() const { return json_error_body; }
private:
std::string json_error_body;
};
TEST(CustomHandlerException, BuilderSample) {
CustomErrorBuilder{"Bad Request", "Something went wrong"});
EXPECT_EQ(exc.what(), std::string{"Client error"}) << "Log message";
R"({"code":"Bad Request","message":"Something went wrong"})");
}
Definition at line 253 of file exceptions.hpp.
◆ CustomHandlerException() [2/4]
server::handlers::CustomHandlerException::CustomHandlerException |
( |
HandlerErrorCode |
handler_code | ) |
|
|
inlineexplicit |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 258 of file exceptions.hpp.
◆ CustomHandlerException() [3/4]
◆ CustomHandlerException() [4/4]
template<typename MessageBuilder , typename = std::enable_if_t<impl::kIsMessageBuilder<MessageBuilder>>>
server::handlers::CustomHandlerException::CustomHandlerException |
( |
MessageBuilder && |
builder, |
|
|
HandlerErrorCode |
handler_code |
|
) |
| |
|
inline |
◆ GetCode()
◆ GetDetails()
◆ GetExternalErrorBody()
const std::string & server::handlers::CustomHandlerException::GetExternalErrorBody |
( |
| ) |
const |
|
inline |
◆ GetExtraHeaders()
const Headers & server::handlers::CustomHandlerException::GetExtraHeaders |
( |
| ) |
const |
|
inline |
◆ GetServiceCode()
const std::string & server::handlers::CustomHandlerException::GetServiceCode |
( |
| ) |
const |
|
inline |
◆ IsExternalErrorBodyFormatted()
bool server::handlers::CustomHandlerException::IsExternalErrorBodyFormatted |
( |
| ) |
const |
|
inline |
The documentation for this class was generated from the following file: