userver: userver/clients/http/error.hpp Source File
Loading...
Searching...
No Matches
error.hpp
1#pragma once
2
3#include <exception>
4#include <string>
5#include <string_view>
6#include <system_error>
7
8#include <userver/clients/http/local_stats.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace clients::http {
13
14/// Exception with string
15class BaseException : public std::exception {
16 public:
17 BaseException() = default;
18 BaseException(std::string message, const LocalStats& stats)
19 : message_(std::move(message)), stats_(stats) {}
20 ~BaseException() override = default;
21
22 const char* what() const noexcept override { return message_.c_str(); }
23
24 const LocalStats& GetStats() const { return stats_; }
25
26 private:
27 std::string message_;
28 LocalStats stats_;
29};
30
31/// Exception with string and error_code
32class BaseCodeException : public BaseException {
33 public:
34 BaseCodeException(std::error_code ec, std::string_view message,
35 std::string_view url, const LocalStats& stats);
36 ~BaseCodeException() override = default;
37
38 const std::error_code& error_code() const noexcept { return ec_; }
39
40 private:
41 std::error_code ec_;
42};
43
44class TimeoutException : public BaseException {
45 public:
46 using BaseException::BaseException;
47 ~TimeoutException() override = default;
48};
49
50class CancelException : public BaseException {
51 public:
52 using BaseException::BaseException;
53 ~CancelException() override = default;
54};
55
56class SSLException : public BaseCodeException {
57 public:
58 using BaseCodeException::BaseCodeException;
59 ~SSLException() override = default;
60};
61
62class TechnicalError : public BaseCodeException {
63 public:
64 using BaseCodeException::BaseCodeException;
65 ~TechnicalError() override = default;
66};
67
69 public:
70 using BaseCodeException::BaseCodeException;
71 ~BadArgumentException() override = default;
72};
73
75 public:
76 using BaseCodeException::BaseCodeException;
77 ~TooManyRedirectsException() override = default;
78};
79
81 public:
82 using BaseCodeException::BaseCodeException;
83 ~NetworkProblemException() override = default;
84};
85
87 public:
88 using BaseCodeException::BaseCodeException;
89 ~DNSProblemException() override = default;
90};
91
93 public:
94 using BaseCodeException::BaseCodeException;
95 ~AuthFailedException() override = default;
96};
97
98/// Base class for HttpClientException and HttpServerException
99class HttpException : public BaseException {
100 public:
101 HttpException(int code, const LocalStats& stats);
102 HttpException(int code, const LocalStats& stats, std::string_view message);
103 ~HttpException() override = default;
104
105 int code() const { return code_; }
106
107 private:
108 int code_;
109};
110
111class HttpClientException : public HttpException {
112 public:
113 using HttpException::HttpException;
114 ~HttpClientException() override = default;
115};
116
117class HttpServerException : public HttpException {
118 public:
119 using HttpException::HttpException;
120 ~HttpServerException() override = default;
121};
122
123/// map error_code to exceptions
124std::exception_ptr PrepareException(std::error_code ec, std::string_view url,
125 const LocalStats& stats);
126
127} // namespace clients::http
128
129USERVER_NAMESPACE_END