error.cc
1 // Copyright 2019, Beeri 15. All rights reserved.
2 // Author: Roman Gershman (romange@gmail.com)
3 //
4 
5 #include "util/asio/error.h"
6 #include "absl/strings/str_cat.h"
7 
8 namespace util {
9 
10 using namespace boost;
11 using namespace std;
12 
13 namespace {
14 
15 class gaia_error_category : public system::error_category {
16  public:
17  const char* name() const noexcept final { return "gaia.util"; }
18 
19  std::string message(int ev) const override {
20  switch (static_cast<gaia_error>(ev)) {
21  case gaia_error::bad_header:
22  return "bad header";
23  case gaia_error::invalid_version:
24  return "invalid header version";
25  default:
26  return absl::StrCat("gaia.util error(", ev, ")");
27  }
28  }
29 
30  system::error_condition default_error_condition(int ev) const noexcept override {
31  return system::error_condition{ev, *this};
32  }
33 
34  bool equivalent(int ev, system::error_condition const& condition) const noexcept override {
35  return condition.value() == ev && &condition.category() == this;
36  }
37 
38  bool equivalent(system::error_code const& error, int ev) const noexcept override {
39  return error.value() == ev && &error.category() == this;
40  }
41 };
42 
43 } // namespace
44 
45 system::error_code make_error_code(gaia_error ev) {
46  static const gaia_error_category cat;
47  return system::error_code{static_cast<int>(ev), cat};
48 }
49 
50 } // namespace util