1 /**************************************************************************
2    Copyright (c) 2017 sewenew
3 
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7 
8        http://www.apache.org/licenses/LICENSE-2.0
9 
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15  *************************************************************************/
16 
17 #include "shards.h"
18 
19 namespace sw {
20 
21 namespace redis {
22 
RedirectionError(const std::string & msg)23 RedirectionError::RedirectionError(const std::string &msg): ReplyError(msg) {
24     std::tie(_slot, _node) = _parse_error(msg);
25 }
26 
_parse_error(const std::string & msg) const27 std::pair<Slot, Node> RedirectionError::_parse_error(const std::string &msg) const {
28     // "slot ip:port"
29     auto space_pos = msg.find(" ");
30     auto colon_pos = msg.find(":");
31     if (space_pos == std::string::npos
32             || colon_pos == std::string::npos
33             || colon_pos < space_pos) {
34         throw ProtoError("Invalid ASK error message: " + msg);
35     }
36 
37     try {
38         auto slot = std::stoull(msg.substr(0, space_pos));
39         auto host = msg.substr(space_pos + 1, colon_pos - space_pos - 1);
40         auto port = std::stoi(msg.substr(colon_pos + 1));
41 
42         return {slot, {host, port}};
43     } catch (const std::exception &e) {
44         throw ProtoError("Invalid ASK error message: " + msg);
45     }
46 }
47 
48 }
49 
50 }
51