userver: userver/server/handlers/json_error_builder.hpp Source File
Loading...
Searching...
No Matches
json_error_builder.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/handlers/json_error_builder.hpp
4/// @brief @copybrief server::handlers::JsonErrorBuilder
5
6#include <userver/formats/json/value.hpp>
7#include <userver/http/content_type.hpp>
8
9#include <userver/server/handlers/exceptions.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace server::handlers {
14
15/// JSON error message builder.
16/// Useful for handlers derived from HttpHandlerBase but responding via JSON.
18public:
19 static constexpr bool kIsExternalBodyFormatted = true;
20
21 explicit JsonErrorBuilder(const CustomHandlerException& ex);
22
23 JsonErrorBuilder(
24 std::string_view error_code,
25 std::string internal_message,
26 std::string_view external_error_body,
27 formats::json::Value = {}
28 );
29
30 const std::string& GetInternalMessage() const { return internal_message_; };
31
32 const std::string& GetExternalBody() const { return json_error_body_; }
33
34 static const USERVER_NAMESPACE::http::ContentType& GetContentType() {
35 return USERVER_NAMESPACE::http::content_type::kApplicationJson;
36 }
37
38private:
39 std::string internal_message_;
40 std::string json_error_body_;
41};
42
43} // namespace server::handlers
44
45USERVER_NAMESPACE_END