userver: userver/http/parse_status_code.hpp Source File
Loading...
Searching...
No Matches
parse_status_code.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/http/parse_status_code.hpp
4/// @brief @copybrief http::Parse
5/// @ingroup userver_universal
6
7#include <fmt/format.h>
8
9#include <userver/formats/common/meta.hpp>
10#include <userver/formats/parse/to.hpp>
11#include <userver/http/status_code.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace http {
16
17/// @brief Parsing helpers for HTTP status codes from format values.
18template <formats::common::kIsFormatValue Value>
19StatusCode Parse(const Value& value, formats::parse::To<StatusCode>) {
20 using IntType = std::underlying_type_t<StatusCode>;
21 constexpr IntType kMinCode = 100;
22 constexpr IntType kMaxCode = 599;
23
24 const auto integer = value.template As<IntType>();
25 if (integer < kMinCode || integer > kMaxCode) {
26 throw typename Value::Exception(fmt::format(
27 "StatusCode value {} out of [{}..{}] range at '{}'",
28 integer,
29 kMinCode,
30 kMaxCode,
31 value.GetPath()
32 ));
33 }
34
35 return StatusCode{integer};
36}
37
38} // namespace http
39
40USERVER_NAMESPACE_END