Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Error.cpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#include "Error.hpp"
18
19namespace toolbox {
20using namespace std::literals::string_literals;
21inline namespace http {
22namespace {
23struct ErrorCategory final : std::error_category {
24 constexpr ErrorCategory() noexcept = default;
25 ~ErrorCategory() override = default;
26
27 // Copy.
28 ErrorCategory(const ErrorCategory&) = delete;
29 ErrorCategory& operator=(const ErrorCategory&) = delete;
30
31 // Move.
32 ErrorCategory(ErrorCategory&&) = delete;
33 ErrorCategory& operator=(ErrorCategory&&) = delete;
34
35 const char* name() const noexcept override { return "http"; }
36 std::string message(int err) const override { return enum_string(static_cast<Status>(err)); }
37};
38
39const ErrorCategory ecat_{};
40} // namespace
41
42const std::error_category& error_category() noexcept
43{
44 return ecat_;
45}
46
48{
49 return {static_cast<int>(status), ecat_};
50}
51
52Status http_status(const std::error_code& ec)
53{
54 if (ec.category() != error_category()) {
56 }
57 return static_cast<Status>(ec.value());
58}
59
60} // namespace http
61} // namespace toolbox
Status http_status(const std::error_code &ec)
Definition Error.cpp:52
const std::error_category & error_category() noexcept
Definition Error.cpp:42
std::error_code make_error_code(Status status)
Definition Error.cpp:47
const char * enum_string(Status status) noexcept
Definition Types.cpp:22
constexpr auto bind() noexcept
Definition Slot.hpp:92