diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h index 5f7490f86..74a27af5d 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h @@ -153,7 +153,7 @@ class HTTPClient { int writeToStream(Stream * stream); String getString(void); - String errorToString(int error); + static String errorToString(int error); protected: diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino b/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino index bc9160a6e..c7da7139b 100644 --- a/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino +++ b/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino @@ -45,7 +45,7 @@ void loop() { switch(ret) { case HTTP_UPDATE_FAILED: - USE_SERIAL.println("HTTP_UPDATE_FAILD"); + USE_SERIAL.printf("HTTP_UPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); break; case HTTP_UPDATE_NO_UPDATES: diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino b/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino index 3d746af6c..a993386cf 100644 --- a/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino +++ b/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino @@ -48,7 +48,7 @@ void loop() { switch(ret) { case HTTP_UPDATE_FAILED: - USE_SERIAL.println("HTTP_UPDATE_FAILD"); + USE_SERIAL.printf("HTTP_UPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); break; case HTTP_UPDATE_NO_UPDATES: diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp index 2eacf09db..4ee378f47 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp @@ -82,6 +82,54 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String return handleUpdate(&http, current_version.c_str(), reboot, false); } +/** + * return error code as int + * @return int error code + */ +int ESP8266HTTPUpdate::getLastError(void){ + return lastError; +} + +/** + * return error code as String + * @return String error + */ +String ESP8266HTTPUpdate::getLastErrorString(void) { + + if(lastError == 0) { + return String(); // no error + } + + // error from Update class + if(lastError > 0) { + StreamString error; + Update.printError(error); + error.trim(); // remove line ending + return "Update error: " + error; + } + + // error from http client + if(lastError > -100) { + return "HTTP error: " + HTTPClient::errorToString(lastError); + } + + switch(lastError) { + case HTTP_UE_TOO_LESS_SPACE: + return String("To less space"); + case HTTP_UE_SERVER_NOT_REPORT_SIZE: + return String("Server not Report Size"); + case HTTP_UE_SERVER_FILE_NOT_FOUND: + return String("File not Found (404)"); + case HTTP_UE_SERVER_FORBIDDEN: + return String("Forbidden (403)"); + case HTTP_UE_SERVER_WRONG_HTTP_CODE: + return String("Wrong HTTP code"); + } + + return String(); +} + + /** * * @param http HTTPClient * @@ -122,10 +170,12 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha if(code <= 0) { DEBUG_HTTP_UPDATE("[httpUpdate] HTTP error: %s\n", http->errorToString(code).c_str()); + lastError = code; http->end(); return HTTP_UPDATE_FAILED; } + DEBUG_HTTP_UPDATE("[httpUpdate] Header read fin.\n"); DEBUG_HTTP_UPDATE("[httpUpdate] Server header:\n"); DEBUG_HTTP_UPDATE("[httpUpdate] - code: %d\n", code); @@ -161,6 +211,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha } if(!startUpdate) { + lastError = HTTP_UE_TOO_LESS_SPACE; ret = HTTP_UPDATE_FAILED; } else { @@ -196,6 +247,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha } } } else { + lastError = HTTP_UE_SERVER_NOT_REPORT_SIZE; ret = HTTP_UPDATE_FAILED; DEBUG_HTTP_UPDATE("[httpUpdate] Content-Length is 0 or not set by Server?!\n"); } @@ -204,16 +256,23 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha ///< Not Modified (No updates) ret = HTTP_UPDATE_NO_UPDATES; break; + case HTTP_CODE_NOT_FOUND: + lastError = HTTP_UE_SERVER_FILE_NOT_FOUND; + ret = HTTP_UPDATE_FAILED; + break; + case HTTP_CODE_FORBIDDEN: + lastError = HTTP_UE_SERVER_FORBIDDEN; + ret = HTTP_UPDATE_FAILED; + break; default: + lastError = HTTP_UE_SERVER_WRONG_HTTP_CODE; ret = HTTP_UPDATE_FAILED; DEBUG_HTTP_UPDATE("[httpUpdate] HTTP Code is (%d)\n", code); //http->writeToStream(&Serial1); break; } - http->end(); - return ret; } @@ -229,6 +288,7 @@ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int com StreamString error; if(!Update.begin(size, command)) { + lastError = Update.getError(); Update.printError(error); error.trim(); // remove line ending DEBUG_HTTP_UPDATE("[httpUpdate] Update.begin failed! (%s)\n", error.c_str()); @@ -240,6 +300,7 @@ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int com } if(Update.writeStream(in) != size) { + lastError = Update.getError(); Update.printError(error); error.trim(); // remove line ending DEBUG_HTTP_UPDATE("[httpUpdate] Update.writeStream failed! (%s)\n", error.c_str()); @@ -247,6 +308,7 @@ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int com } if(!Update.end()) { + lastError = Update.getError(); Update.printError(error); error.trim(); // remove line ending DEBUG_HTTP_UPDATE("[httpUpdate] Update.end failed! (%s)\n", error.c_str()); diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h index 6a13f9ce3..c8061c715 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h @@ -38,6 +38,13 @@ #define DEBUG_HTTP_UPDATE(...) #endif +/// note we use HTTP client errors too so we start at 100 +#define HTTP_UE_TOO_LESS_SPACE (-100) +#define HTTP_UE_SERVER_NOT_REPORT_SIZE (-101) +#define HTTP_UE_SERVER_FILE_NOT_FOUND (-102) +#define HTTP_UE_SERVER_FORBIDDEN (-103) +#define HTTP_UE_SERVER_WRONG_HTTP_CODE (-104) + typedef enum { HTTP_UPDATE_FAILED, HTTP_UPDATE_NO_UPDATES, @@ -55,9 +62,14 @@ class ESP8266HTTPUpdate { t_httpUpdate_return updateSpiffs(const char * url, const char * current_version = "", const char * httpsFingerprint = "", bool reboot = false); + int getLastError(void); + String getLastErrorString(void); + protected: t_httpUpdate_return handleUpdate(HTTPClient * http, const char * current_version, bool reboot = true, bool spiffs = false); bool runUpdate(Stream& in, uint32_t size, String md5, int command = U_FLASH); + + int lastError; }; extern ESP8266HTTPUpdate ESPhttpUpdate;