forked from sewenew/redis-plus-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.cpp
142 lines (105 loc) · 3.37 KB
/
errors.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**************************************************************************
Copyright (c) 2017 sewenew
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************/
#include "errors.h"
#include <cassert>
#include <cerrno>
#include <unordered_map>
#include <tuple>
#include "shards.h"
namespace {
using namespace sw::redis;
std::pair<ReplyErrorType, std::string> parse_error(const std::string &msg);
std::unordered_map<std::string, ReplyErrorType> error_map = {
{"MOVED", ReplyErrorType::MOVED},
{"ASK", ReplyErrorType::ASK}
};
}
namespace sw {
namespace redis {
void throw_error(const redisContext &context, const std::string &err_info) {
auto err_code = context.err;
const auto *err_str = context.errstr;
if (err_str == nullptr) {
throw Error(err_info + ": null error message: " + std::to_string(err_code));
}
auto err_msg = err_info + ": " + err_str;
switch (err_code) {
case REDIS_ERR_IO:
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ETIMEDOUT) {
throw TimeoutError(err_msg);
} else {
throw IoError(err_msg);
}
break;
case REDIS_ERR_EOF:
throw ClosedError(err_msg);
break;
case REDIS_ERR_PROTOCOL:
throw ProtoError(err_msg);
break;
case REDIS_ERR_OOM:
throw OomError(err_msg);
break;
case REDIS_ERR_OTHER:
throw Error(err_msg);
break;
#ifdef REDIS_ERR_TIMEOUT
case REDIS_ERR_TIMEOUT:
throw TimeoutError(err_msg);
break;
#endif
default:
throw Error("unknown error code: " + err_msg);
}
}
void throw_error(const redisReply &reply) {
assert(reply.type == REDIS_REPLY_ERROR);
if (reply.str == nullptr) {
throw Error("Null error reply");
}
auto err_str = std::string(reply.str, reply.len);
auto err_type = ReplyErrorType::ERR;
std::string err_msg;
std::tie(err_type, err_msg) = parse_error(err_str);
switch (err_type) {
case ReplyErrorType::MOVED:
throw MovedError(err_msg);
break;
case ReplyErrorType::ASK:
throw AskError(err_msg);
break;
default:
throw ReplyError(err_str);
break;
}
}
}
}
namespace {
using namespace sw::redis;
std::pair<ReplyErrorType, std::string> parse_error(const std::string &err) {
// The error contains an Error Prefix, and an optional error message.
auto idx = err.find_first_of(" \n");
if (idx == std::string::npos) {
throw ProtoError("No Error Prefix: " + err);
}
auto err_prefix = err.substr(0, idx);
auto err_type = ReplyErrorType::ERR;
auto iter = error_map.find(err_prefix);
if (iter != error_map.end()) {
// Specific error.
err_type = iter->second;
} // else Generic error.
return {err_type, err.substr(idx + 1)};
}
}