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