1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

add advanced error handling for HTTP update

see: #1111
This commit is contained in:
Markus Sattler 2015-12-16 23:05:23 +01:00
parent b2de8735c8
commit 28dd31a80f
5 changed files with 79 additions and 5 deletions

View File

@ -153,7 +153,7 @@ class HTTPClient {
int writeToStream(Stream * stream);
String getString(void);
String errorToString(int error);
static String errorToString(int error);
protected:

View File

@ -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:

View File

@ -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:

View File

@ -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());

View File

@ -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;