-
Notifications
You must be signed in to change notification settings - Fork 1
/
failure_source.cpp
97 lines (82 loc) · 2.69 KB
/
failure_source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "failure_source.h"
#include "flights_err.h"
#include "seats_err.h"
namespace flightservice {
namespace { // anonymous namespace for category definition
struct FailureSourceCategory : public std::error_category {
const char* name() const noexcept override;
std::string message(int ev) const override;
bool equivalent(const std::error_code& code,
int condition) const noexcept override;
};
const char* FailureSourceCategory::name() const noexcept
{
return "FailureSource";
}
std::string FailureSourceCategory::message(int ev) const
{
switch (static_cast<FailureSource>(ev)) {
case FailureSource::BadUserInput:
return "invalid user request";
case FailureSource::InternalError:
return "internal error";
case FailureSource::NoSolution:
return "no solution found for specified request";
default:
break;
}
return "(unrecognized error)";
}
bool FailureSourceCategory::equivalent(const std::error_code& ec,
int condition) const noexcept
{
switch (static_cast<FailureSource>(condition)) {
case FailureSource::BadUserInput: {
// for SeatsErr
if (ec == SeatsErr::NonexistentClass) {
return true;
}
// for FlightsErr
if (ec.category() == get_flights_err_category()) {
return ec.value() >= 10 && ec.value() < 20;
}
// for any other error_code enum:
return false;
}
case FailureSource::InternalError: {
// for SeatsErr
if (ec.category() == get_seats_err_category()) {
return ec.value() >= 1 && ec.value() < 10;
}
// for FlightsErr
if (ec.category() == get_flights_err_category()) {
return ec.value() >= 30;
}
// for any other error_code enum:
return false;
}
case FailureSource::NoSolution: {
// for SeatsErr
if (ec == SeatsErr::NoSeatAvailable) {
return true;
}
// for FlightsErr
if (ec == FlightsErr::NoFlightsFound) {
return true;
}
// for any other error_code enum:
return false;
}
default:
break;
}
return false;
}
// global object for unify this category
const FailureSourceCategory the_failure_source_category {};
}
std::error_condition make_error_condition(FailureSource e) noexcept
{
return { static_cast<int>(e), the_failure_source_category };
}
}