Commit a4c67dcc093b50c61f3873013c610f486a8e7bc6
Committed by
Moritz Wirger
1 parent
463bc8ba
Improve error messages in LinHttpHandler
- improved by also printing errno as string
Showing
1 changed file
with
19 additions
and
19 deletions
hueplusplus/LinHttpHandler.cpp
| ... | ... | @@ -20,6 +20,7 @@ |
| 20 | 20 | #include "include/LinHttpHandler.h" |
| 21 | 21 | |
| 22 | 22 | #include <chrono> |
| 23 | +#include <cstring> | |
| 23 | 24 | #include <netinet/in.h> // struct sockaddr_in, struct sockaddr |
| 24 | 25 | #include <arpa/inet.h> |
| 25 | 26 | #include <iostream> |
| ... | ... | @@ -46,8 +47,8 @@ std::string LinHttpHandler::send(const std::string & msg, const std::string & ad |
| 46 | 47 | SocketCloser closeMySocket(socketFD); |
| 47 | 48 | if (socketFD < 0) |
| 48 | 49 | { |
| 49 | - std::cerr << "LinHttpHandler: Failed to open socket\n"; | |
| 50 | - throw(std::runtime_error("LinHttpHandler: Failed to open socket")); | |
| 50 | + std::cerr << "linHttpHandler: Failed to open socket: " << std::strerror(errno) << "\n"; | |
| 51 | + throw(std::runtime_error("linHttpHandler: Failed to open socket")); | |
| 51 | 52 | } |
| 52 | 53 | |
| 53 | 54 | // lookup ip address |
| ... | ... | @@ -55,8 +56,8 @@ std::string LinHttpHandler::send(const std::string & msg, const std::string & ad |
| 55 | 56 | server = gethostbyname(adr.c_str()); |
| 56 | 57 | if (server == NULL) |
| 57 | 58 | { |
| 58 | - std::cerr << "LinHttpHandler: Failed to find host with address " << adr << "\n"; | |
| 59 | - throw(std::runtime_error("LinHttpHandler: Failed to find host")); | |
| 59 | + std::cerr << "linHttpHandler: Failed to find host with address " << adr << ": " << std::strerror(errno) << "\n"; | |
| 60 | + throw(std::runtime_error("linHttpHandler: Failed to find host")); | |
| 60 | 61 | } |
| 61 | 62 | |
| 62 | 63 | // fill in the structure |
| ... | ... | @@ -69,8 +70,8 @@ std::string LinHttpHandler::send(const std::string & msg, const std::string & ad |
| 69 | 70 | // connect the socket |
| 70 | 71 | if (connect(socketFD, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) |
| 71 | 72 | { |
| 72 | - std::cerr << "LinHttpHandler: Failed to connect socket\n"; | |
| 73 | - throw(std::runtime_error("LinHttpHandler: Failed to connect socket")); | |
| 73 | + std::cerr << "linHttpHandler: Failed to connect socket: " << std::strerror(errno) << "\n"; | |
| 74 | + throw(std::runtime_error("linHttpHandler: Failed to connect socket")); | |
| 74 | 75 | } |
| 75 | 76 | |
| 76 | 77 | // send the request |
| ... | ... | @@ -81,8 +82,8 @@ std::string LinHttpHandler::send(const std::string & msg, const std::string & ad |
| 81 | 82 | ssize_t bytes = write(socketFD, msg.c_str() + sent, total - sent); |
| 82 | 83 | if (bytes < 0) |
| 83 | 84 | { |
| 84 | - std::cerr << "LinHttpHandler: Failed to write message to socket\n"; | |
| 85 | - throw(std::runtime_error("LinHttpHandler: Failed to write message to socket")); | |
| 85 | + std::cerr << "linHttpHandler: Failed to write message to socket: " << std::strerror(errno) << "\n"; | |
| 86 | + throw(std::runtime_error("linHttpHandler: Failed to write message to socket")); | |
| 86 | 87 | } |
| 87 | 88 | if (bytes == 0) |
| 88 | 89 | { |
| ... | ... | @@ -104,8 +105,8 @@ std::string LinHttpHandler::send(const std::string & msg, const std::string & ad |
| 104 | 105 | ssize_t bytes = read(socketFD, buffer, 127); |
| 105 | 106 | if (bytes < 0) |
| 106 | 107 | { |
| 107 | - std::cerr << "LinHttpHandler: Failed to read response from socket: " << errno << std::endl; | |
| 108 | - throw(std::runtime_error("LinHttpHandler: Failed to read response from socket")); | |
| 108 | + std::cerr << "linHttpHandler: Failed to read response from socket: " << std::strerror(errno) << std::endl; | |
| 109 | + throw(std::runtime_error("linHttpHandler: Failed to read response from socket")); | |
| 109 | 110 | } |
| 110 | 111 | if (bytes == 0) |
| 111 | 112 | { |
| ... | ... | @@ -141,8 +142,8 @@ std::vector<std::string> LinHttpHandler::sendMulticast(const std::string & msg, |
| 141 | 142 | server = gethostbyname(adr.c_str()); |
| 142 | 143 | if (!server) |
| 143 | 144 | { |
| 144 | - std::cerr << "LinHttpHandler: sendMulticast: Failed to obtain address of " << msg << "\n"; | |
| 145 | - throw(std::runtime_error("LinHttpHandler: sendMulticast: Failed to obtain address of host")); | |
| 145 | + std::cerr << "linHttpHandler: sendMulticast: Failed to obtain address of " << msg << ": " << std::strerror(errno) << "\n"; | |
| 146 | + throw(std::runtime_error("linHttpHandler: sendMulticast: Failed to obtain address of host")); | |
| 146 | 147 | } |
| 147 | 148 | |
| 148 | 149 | // put the host's address into the server address structure |
| ... | ... | @@ -153,15 +154,15 @@ std::vector<std::string> LinHttpHandler::sendMulticast(const std::string & msg, |
| 153 | 154 | SocketCloser closeMySendSocket(socketFD); |
| 154 | 155 | if (socketFD < 0) |
| 155 | 156 | { |
| 156 | - std::cerr << "LinHttpHandler: sendMulticast: Failed to open socket\n"; | |
| 157 | - throw(std::runtime_error("LinHttpHandler: sendMulticast: Failed to open socket")); | |
| 157 | + std::cerr << "linHttpHandler: sendMulticast: Failed to open socket: " << std::strerror(errno) << "\n"; | |
| 158 | + throw(std::runtime_error("linHttpHandler: sendMulticast: Failed to open socket")); | |
| 158 | 159 | } |
| 159 | 160 | |
| 160 | 161 | // send a message to the server |
| 161 | 162 | if (sendto(socketFD, msg.c_str(), strlen(msg.c_str()), 0, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) |
| 162 | 163 | { |
| 163 | - std::cerr << "LinHttpHandler: sendMulticast: Failed to send message\n"; | |
| 164 | - throw(std::runtime_error("LinHttpHandler: sendMulticast: Failed to send message")); | |
| 164 | + std::cerr << "linHttpHandler: sendMulticast: Failed to send message: " << std::strerror(errno) << "\n"; | |
| 165 | + throw(std::runtime_error("linHttpHandler: sendMulticast: Failed to send message")); | |
| 165 | 166 | } |
| 166 | 167 | |
| 167 | 168 | std::string response; |
| ... | ... | @@ -176,8 +177,8 @@ std::vector<std::string> LinHttpHandler::sendMulticast(const std::string & msg, |
| 176 | 177 | { |
| 177 | 178 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
| 178 | 179 | { |
| 179 | - std::cerr << "LinHttpHandler: sendMulticast: Failed to read response from socket\n"; | |
| 180 | - throw(std::runtime_error("LinHttpHandler: sendMulticast: Failed to read response from socket")); | |
| 180 | + std::cerr << "linHttpHandler: sendMulticast: Failed to read response from socket: " << std::strerror(errno) << "\n"; | |
| 181 | + throw(std::runtime_error("linHttpHandler: sendMulticast: Failed to read response from socket")); | |
| 181 | 182 | } |
| 182 | 183 | continue; |
| 183 | 184 | } |
| ... | ... | @@ -198,6 +199,5 @@ std::vector<std::string> LinHttpHandler::sendMulticast(const std::string & msg, |
| 198 | 199 | prevpos = pos; |
| 199 | 200 | pos = response.find("\r\n\r\n", pos); |
| 200 | 201 | } |
| 201 | - | |
| 202 | 202 | return returnString; |
| 203 | 203 | } | ... | ... |