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/http_method.h>
9#include <sourcemeta/core/http_status.h>
10
11#include <cstdint> // std::uint16_t
12#include <stdexcept> // std::runtime_error
13#include <string> // std::string
14#include <utility> // std::move
15
16namespace sourcemeta::core {
17
18// Exporting symbols that depends on the standard C++ library is considered
19// safe.
20// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
21#if defined(_MSC_VER)
22#pragma warning(disable : 4251 4275)
23#endif
24
39class SOURCEMETA_CORE_HTTP_EXPORT HTTPError : public std::runtime_error {
40public:
42 HTTPError(const HTTPMethod method, std::string url,
43 const std::string &message)
44 : std::runtime_error{message}, method_{method}, url_{std::move(url)} {}
45
47 [[nodiscard]] auto method() const noexcept -> HTTPMethod {
48 return this->method_;
49 }
50
52 [[nodiscard]] auto url() const noexcept -> const std::string & {
53 return this->url_;
54 }
55
56private:
57 HTTPMethod method_;
58 std::string url_;
59};
60
74class SOURCEMETA_CORE_HTTP_EXPORT HTTPStatusError : public HTTPError {
75public:
79 const HTTPStatus &status)
80 : HTTPError{method, std::move(url), "Unsuccessful HTTP response"},
81 code_{status.code}, phrase_{status.phrase}, wire_{status.wire} {}
82
85 [[nodiscard]] auto status() const noexcept -> HTTPStatus {
86 return {.code = this->code_, .phrase = this->phrase_, .wire = this->wire_};
87 }
88
89private:
90 std::uint16_t code_;
91 std::string phrase_;
92 std::string wire_;
93};
94
95#if defined(_MSC_VER)
96#pragma warning(default : 4251 4275)
97#endif
98
99} // namespace sourcemeta::core
100
101#endif
auto method() const noexcept -> HTTPMethod
Get the request method that triggered the failure.
Definition http_error.h:47
auto status() const noexcept -> HTTPStatus
Definition http_error.h:85
auto url() const noexcept -> const std::string &
Get the request URL that triggered the failure.
Definition http_error.h:52
HTTPError(const HTTPMethod method, std::string url, const std::string &message)
Construct an error from the request method, URL, and a message.
Definition http_error.h:42
HTTPStatusError(const HTTPMethod method, std::string url, const HTTPStatus &status)
Definition http_error.h:78
HTTPMethod
Definition http_method.h:12
Definition http_status.h:20