Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
http_error.h
1#ifndef SOURCEMETA_CORE_HTTP_ERROR_H_
2#define SOURCEMETA_CORE_HTTP_ERROR_H_
3
4#ifndef SOURCEMETA_CORE_HTTP_EXPORT
5#include <sourcemeta/core/http_export.h>
6#endif
7
8#include <sourcemeta/core/error.h>
9#include <sourcemeta/core/http_method.h>
10#include <sourcemeta/core/http_status.h>
11
12#include <chrono> // std::chrono::milliseconds
13#include <cstdint> // std::uint16_t
14#include <string> // std::string
15#include <string_view> // std::string_view
16#include <utility> // std::move
17
18namespace sourcemeta::core {
19
20SOURCEMETA_CORE_ERROR_WARNINGS_PUSH
21
36class SOURCEMETA_CORE_HTTP_EXPORT HTTPError : public Error {
37public:
40 std::string url)
41 : Error{message}, method_{method}, url_{std::move(url)} {}
42
46 std::string url, std::string detail)
47 : Error{message}, method_{method}, url_{std::move(url)},
48 detail_{std::move(detail)} {}
49
51 [[nodiscard]] auto method() const noexcept -> HTTPMethod {
52 return this->method_;
53 }
54
56 [[nodiscard]] auto url() const noexcept -> const std::string & {
57 return this->url_;
58 }
59
61 [[nodiscard]] auto detail() const noexcept -> std::string_view {
62 return this->detail_;
63 }
64
65private:
66 HTTPMethod method_;
67 std::string url_;
68 std::string detail_;
69};
70
84class SOURCEMETA_CORE_HTTP_EXPORT HTTPTimeoutError : public HTTPError {
85public:
89 const std::chrono::milliseconds timeout)
90 : HTTPError{"The HTTP request timed out", method, std::move(url)},
91 timeout_{timeout} {}
92
96 [[nodiscard]] auto timeout() const noexcept -> std::chrono::milliseconds {
97 return this->timeout_;
98 }
99
100private:
101 std::chrono::milliseconds timeout_;
102};
103
117class SOURCEMETA_CORE_HTTP_EXPORT HTTPStatusError : public HTTPError {
118public:
122 const HTTPStatus &status)
123 : HTTPError{"Unsuccessful HTTP response", method, std::move(url)},
124 code_{status.code}, phrase_{status.phrase}, wire_{status.wire} {}
125
128 [[nodiscard]] auto status() const noexcept -> HTTPStatus {
129 return {.code = this->code_, .phrase = this->phrase_, .wire = this->wire_};
130 }
131
132private:
133 std::uint16_t code_;
134 std::string phrase_;
135 std::string wire_;
136};
137
140class SOURCEMETA_CORE_HTTP_EXPORT HTTPEntropyError : public Error {
141public:
142 HTTPEntropyError() noexcept
143 : Error{"Could not draw a non-zero trace context identifier"} {}
144};
145
146SOURCEMETA_CORE_ERROR_WARNINGS_POP
147
148} // namespace sourcemeta::core
149
150#endif
auto message() const noexcept -> StaticString
Definition error_base.h:78
Error(const StaticString message) noexcept
Construct the error given its message.
Definition error_base.h:84
auto method() const noexcept -> HTTPMethod
Get the request method that triggered the failure.
Definition http_error.h:51
auto status() const noexcept -> HTTPStatus
Definition http_error.h:128
auto timeout() const noexcept -> std::chrono::milliseconds
Definition http_error.h:96
auto url() const noexcept -> const std::string &
Get the request URL that triggered the failure.
Definition http_error.h:56
HTTPTimeoutError(const HTTPMethod method, std::string url, const std::chrono::milliseconds timeout)
Definition http_error.h:88
auto detail() const noexcept -> std::string_view
Get what the backend reported, empty when it reported nothing.
Definition http_error.h:61
HTTPError(const StaticString message, const HTTPMethod method, std::string url, std::string detail)
Definition http_error.h:45
HTTPError(const StaticString message, const HTTPMethod method, std::string url)
Construct an error from a message, the request method, and the URL.
Definition http_error.h:39
HTTPStatusError(const HTTPMethod method, std::string url, const HTTPStatus &status)
Definition http_error.h:121
HTTPMethod
Definition http_method.h:12
Definition http_status.h:20
Definition text_static_string.h:20