userver: server::handlers::CustomHandlerException Class Reference
Loading...
Searching...
No Matches
server::handlers::CustomHandlerException Class Reference

#include <userver/server/handlers/exceptions.hpp>

Detailed Description

The generic base class for handler exceptions. Thrown exceptions should typically derive from ExceptionWithCode instead.

Definition at line 210 of file exceptions.hpp.

+ Inheritance diagram for server::handlers::CustomHandlerException:
+ Collaboration diagram for server::handlers::CustomHandlerException:

Public Types

using HandlerErrorCode = handlers::HandlerErrorCode
 
using ServiceErrorCode = handlers::ServiceErrorCode
 
using InternalMessage = handlers::InternalMessage
 
using ExternalBody = handlers::ExternalBody
 
using ExtraHeaders = handlers::ExtraHeaders
 

Public Member Functions

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::ValueGetDetails () const
 
const Headers & GetExtraHeaders () const
 

Member Typedef Documentation

◆ ExternalBody

◆ ExtraHeaders

◆ HandlerErrorCode

◆ InternalMessage

◆ ServiceErrorCode

Constructor & Destructor Documentation

◆ 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:

TEST(CustomHandlerException, DirectSample) {
server::handlers::InternalMessage{"Spam detected, criterion: too many "
"repetitions (42, we only allow 10)"},
server::handlers::ExternalBody{"Failed to post: spam detected"});
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);
json_error_body = formats::json::ToString(error.ExtractValue());
}
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"})");
EXPECT_EQ(formats::json::FromString(exc.GetExternalErrorBody()), json);
}

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]

server::handlers::CustomHandlerException::CustomHandlerException ( ServiceErrorCode service_code,
ExternalBody external_body,
InternalMessage internal_message,
HandlerErrorCode handler_code,
ExtraHeaders headers = {},
formats::json::Value details = {} )
inline
Deprecated
Use the variadic constructor above instead.

Definition at line 263 of file exceptions.hpp.

◆ CustomHandlerException() [4/4]

template<typename MessageBuilder , typename = std::enable_if_t<impl::kIsMessageBuilder<MessageBuilder>>>
server::handlers::CustomHandlerException::CustomHandlerException ( MessageBuilder && builder,
HandlerErrorCode handler_code )
inline
Deprecated
Use the variadic constructor above instead.

Definition at line 278 of file exceptions.hpp.

Member Function Documentation

◆ GetCode()

HandlerErrorCode server::handlers::CustomHandlerException::GetCode ( ) const
inline

Definition at line 294 of file exceptions.hpp.

◆ GetDetails()

const formats::json::Value & server::handlers::CustomHandlerException::GetDetails ( ) const
inline

Definition at line 306 of file exceptions.hpp.

◆ GetExternalErrorBody()

const std::string & server::handlers::CustomHandlerException::GetExternalErrorBody ( ) const
inline

Definition at line 302 of file exceptions.hpp.

◆ GetExtraHeaders()

const Headers & server::handlers::CustomHandlerException::GetExtraHeaders ( ) const
inline

Definition at line 308 of file exceptions.hpp.

◆ GetServiceCode()

const std::string & server::handlers::CustomHandlerException::GetServiceCode ( ) const
inline

Definition at line 296 of file exceptions.hpp.

◆ IsExternalErrorBodyFormatted()

bool server::handlers::CustomHandlerException::IsExternalErrorBodyFormatted ( ) const
inline

Definition at line 298 of file exceptions.hpp.


The documentation for this class was generated from the following file: