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, typename = std::enable_if_t<formats::common::kIsFormatValue<Value>>>
16StatusCode Parse(const Value& value, formats::parse::To<StatusCode>) {
17 using IntType = std::underlying_type_t<StatusCode>;
18 constexpr IntType kMinCode = 100;
19 constexpr IntType kMaxCode = 599;
20
21 const auto integer = value.template As<IntType>();
22 if (integer < kMinCode || integer > kMaxCode) {
23 throw typename Value::Exception(fmt::format(
24 "StatusCode value {} out of [{}..{}] range at '{}'", integer, kMinCode, kMaxCode, value.GetPath()
25 ));
26 }
27
28 return StatusCode{integer};
29}
30
31} // namespace http
32
33USERVER_NAMESPACE_END