@@ -82,6 +82,54 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String
8282 return handleUpdate (&http, current_version.c_str (), reboot, false );
8383}
8484
85+ /* *
86+ * return error code as int
87+ * @return int error code
88+ */
89+ int ESP8266HTTPUpdate::getLastError (void ){
90+ return lastError;
91+ }
92+
93+ /* *
94+ * return error code as String
95+ * @return String error
96+ */
97+ String ESP8266HTTPUpdate::getLastErrorString (void ) {
98+
99+ if (lastError == 0 ) {
100+ return String (); // no error
101+ }
102+
103+ // error from Update class
104+ if (lastError > 0 ) {
105+ StreamString error;
106+ Update.printError (error);
107+ error.trim (); // remove line ending
108+ return " Update error: " + error;
109+ }
110+
111+ // error from http client
112+ if (lastError > -100 ) {
113+ return " HTTP error: " + HTTPClient::errorToString (lastError);
114+ }
115+
116+ switch (lastError) {
117+ case HTTP_UE_TOO_LESS_SPACE :
118+ return String (" To less space" );
119+ case HTTP_UE_SERVER_NOT_REPORT_SIZE :
120+ return String (" Server not Report Size" );
121+ case HTTP_UE_SERVER_FILE_NOT_FOUND :
122+ return String (" File not Found (404)" );
123+ case HTTP_UE_SERVER_FORBIDDEN :
124+ return String (" Forbidden (403)" );
125+ case HTTP_UE_SERVER_WRONG_HTTP_CODE :
126+ return String (" Wrong HTTP code" );
127+ }
128+
129+ return String ();
130+ }
131+
132+
85133/* *
86134 *
87135 * @param http HTTPClient *
@@ -122,10 +170,12 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
122170
123171 if (code <= 0 ) {
124172 DEBUG_HTTP_UPDATE (" [httpUpdate] HTTP error: %s\n " , http->errorToString (code).c_str ());
173+ lastError = code;
125174 http->end ();
126175 return HTTP_UPDATE_FAILED ;
127176 }
128177
178+
129179 DEBUG_HTTP_UPDATE (" [httpUpdate] Header read fin.\n " );
130180 DEBUG_HTTP_UPDATE (" [httpUpdate] Server header:\n " );
131181 DEBUG_HTTP_UPDATE (" [httpUpdate] - code: %d\n " , code);
@@ -161,6 +211,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
161211 }
162212
163213 if (!startUpdate) {
214+ lastError = HTTP_UE_TOO_LESS_SPACE ;
164215 ret = HTTP_UPDATE_FAILED ;
165216 } else {
166217
@@ -196,6 +247,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
196247 }
197248 }
198249 } else {
250+ lastError = HTTP_UE_SERVER_NOT_REPORT_SIZE ;
199251 ret = HTTP_UPDATE_FAILED ;
200252 DEBUG_HTTP_UPDATE (" [httpUpdate] Content-Length is 0 or not set by Server?!\n " );
201253 }
@@ -204,16 +256,23 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
204256 // /< Not Modified (No updates)
205257 ret = HTTP_UPDATE_NO_UPDATES ;
206258 break ;
259+ case HTTP_CODE_NOT_FOUND :
260+ lastError = HTTP_UE_SERVER_FILE_NOT_FOUND ;
261+ ret = HTTP_UPDATE_FAILED ;
262+ break ;
263+ case HTTP_CODE_FORBIDDEN :
264+ lastError = HTTP_UE_SERVER_FORBIDDEN ;
265+ ret = HTTP_UPDATE_FAILED ;
266+ break ;
207267 default :
268+ lastError = HTTP_UE_SERVER_WRONG_HTTP_CODE ;
208269 ret = HTTP_UPDATE_FAILED ;
209270 DEBUG_HTTP_UPDATE (" [httpUpdate] HTTP Code is (%d)\n " , code);
210271 // http->writeToStream(&Serial1);
211272 break ;
212273 }
213274
214-
215275 http->end ();
216-
217276 return ret;
218277}
219278
@@ -229,6 +288,7 @@ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int com
229288 StreamString error;
230289
231290 if (!Update.begin (size, command)) {
291+ lastError = Update.getError ();
232292 Update.printError (error);
233293 error.trim (); // remove line ending
234294 DEBUG_HTTP_UPDATE (" [httpUpdate] Update.begin failed! (%s)\n " , error.c_str ());
@@ -240,13 +300,15 @@ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int com
240300 }
241301
242302 if (Update.writeStream (in) != size) {
303+ lastError = Update.getError ();
243304 Update.printError (error);
244305 error.trim (); // remove line ending
245306 DEBUG_HTTP_UPDATE (" [httpUpdate] Update.writeStream failed! (%s)\n " , error.c_str ());
246307 return false ;
247308 }
248309
249310 if (!Update.end ()) {
311+ lastError = Update.getError ();
250312 Update.printError (error);
251313 error.trim (); // remove line ending
252314 DEBUG_HTTP_UPDATE (" [httpUpdate] Update.end failed! (%s)\n " , error.c_str ());
0 commit comments