Skip to content

Commit b1e3d22

Browse files
committed
add errorToString function
1 parent 1dabac6 commit b1e3d22

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void loop() {
6161
USE_SERIAL.println(payload);
6262
}
6363
} else {
64-
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
64+
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
6565
}
6666

6767
http.end();

libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void loop() {
5656
http.writeToStream(&USE_SERIAL);
5757
}
5858
} else {
59-
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
59+
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
6060
}
6161

6262
http.end();

libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void loop() {
9090

9191
}
9292
} else {
93-
USE_SERIAL.print("[HTTP] GET... failed, no connection or no HTTP server\n");
93+
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
9494
}
9595

9696
http.end();

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,33 @@ String HTTPClient::getString(void) {
454454
return sstring;
455455
}
456456

457+
/**
458+
* converts error code to String
459+
* @param error int
460+
* @return String
461+
*/
462+
String HTTPClient::errorToString(int error) {
463+
switch(error) {
464+
case HTTPC_ERROR_CONNECTION_REFUSED:
465+
return String("connection refused");
466+
case HTTPC_ERROR_SEND_HEADER_FAILED:
467+
return String("send header failed");
468+
case HTTPC_ERROR_SEND_PAYLOAD_FAILED:
469+
return String("send payload failed");
470+
case HTTPC_ERROR_NOT_CONNECTED:
471+
return String("not connected");
472+
case HTTPC_ERROR_CONNECTION_LOST:
473+
return String("connection lost");
474+
case HTTPC_ERROR_NO_STREAM:
475+
return String("no stream");
476+
case HTTPC_ERROR_NO_HTTP_SERVER:
477+
return String("no HTTP server");
478+
default:
479+
return String();
480+
}
481+
}
482+
483+
457484
/**
458485
* adds Header to the request
459486
* @param name

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ class HTTPClient {
147147
int writeToStream(Stream * stream);
148148
String getString(void);
149149

150+
String errorToString(int error);
151+
150152
protected:
151153

152154
struct RequestArgument {

0 commit comments

Comments
 (0)