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 {
15 public:
16 static constexpr bool kIsExternalBodyFormatted = true;
17
18 explicit JsonErrorBuilder(const CustomHandlerException& ex);
19
20 JsonErrorBuilder(std::string_view error_code, std::string internal_message,
21 std::string_view external_error_body,
22 formats::json::Value = {});
23
24 const std::string& GetInternalMessage() const { return internal_message_; };
25
26 const std::string& GetExternalBody() const { return json_error_body_; }
27
28 static const USERVER_NAMESPACE::http::ContentType& GetContentType() {
29 return USERVER_NAMESPACE::http::content_type::kApplicationJson;
30 }
31
32 private:
33 std::string internal_message_;
34 std::string json_error_body_;
35};
36
37} // namespace server::handlers
38
39USERVER_NAMESPACE_END