userver: userver/server/http/http_error.hpp Source File
Loading...
Searching...
No Matches
http_error.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/http/http_error.hpp
4/// @brief @copybrief server::http::GetHttpStatus
5
6#include <stdexcept>
7#include <string>
8
9#include <userver/server/handlers/exceptions.hpp>
10#include <userver/server/http/http_status.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace server::http {
15
16/**
17 * @brief Get http status code mapped to generic handler error code.
18 *
19 * If there is no explicit mapping in the http_error.cpp, will return
20 * HttpStatus::kBadRequest for code values less than
21 * HandlerErrorCode::kServerSideError and will return
22 * HttpStatus::kInternalServerError for the rest of codes.
23 */
24HttpStatus GetHttpStatus(handlers::HandlerErrorCode) noexcept;
25
26/// For server::http::CustomHandlerException, uses the provided HttpStatus.
27/// For a generic server::handler::CustomHandlerException, converts its
28/// server::handler::HandlerErrorCode to HttpStatus.
29HttpStatus GetHttpStatus(const handlers::CustomHandlerException& exception) noexcept;
30
31/// @brief An extension of server::handlers::CustomHandlerException that allows
32/// to specify a custom HttpStatus.
33///
34/// For non-HTTP protocols, the status code will be derived from the attached
35/// server::handlers::HandlerErrorCode.
37public:
38 /// @see server::handlers::CustomHandlerException for the description of how
39 /// `args` that can augment error messages.
40 /// @snippet server/handlers/exceptions_test.cpp Sample construction HTTP
41 template <typename... Args>
42 CustomHandlerException(handlers::HandlerErrorCode error_code, HttpStatus http_status, Args&&... args)
43 : handlers::CustomHandlerException(error_code, std::forward<Args>(args)...), http_status_(http_status) {}
44
45 HttpStatus GetHttpStatus() const { return http_status_; }
46
47private:
48 HttpStatus http_status_;
49};
50
51} // namespace server::http
52
53USERVER_NAMESPACE_END