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