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