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