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