Skip to content

Commit fa6847e

Browse files
committed
The first Uncaught error is now saved to a file in Storage called ERROR (E.setFlags({noErrorSave:true}) disables this) (#2583)
1 parent 2ddef19 commit fa6847e

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Error.stack/stack dump now uses more standard file:line:col format
1010
Out of memory errors now show a backtrace (previously it was very hard to track these down)
1111
Increase chars shown in strings in console to 60
12+
The first Uncaught error is now saved to a file in Storage called ERROR (`E.setFlags({noErrorSave:true})` disables this) (#2583)
1213

1314
2v26 : nRF5x: ensure TIMER1_IRQHandler doesn't always wake idle loop up (fix #1900)
1415
Puck.js: On v2.1 ensure Puck.mag behaves like other variants - just returning the last reading (avoids glitches when used with Puck.magOn)

src/jsflags.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ typedef enum {
2525
JSF_PRETOKENISE = 1<<3, ///< When adding functions, pre-minify them and tokenise reserved words (adding "ram" at the start does this too)
2626
#endif
2727
#ifdef ESPR_JIT
28-
JSF_JIT_DEBUG = 1<<4, ///< When JIT enabled,
28+
JSF_JIT_DEBUG = 1<<4, ///< When JIT enabled, output debugging info
2929
#endif
30+
JSF_NO_ERRORS_SAVE = 1<<5, ///< If set, do not attempt to save errors to internal Storage
3031
} PACKED_FLAGS JsFlags;
3132

3233

33-
#define JSFLAG_NAMES "deepSleep\0unsafeFlash\0unsyncFiles\0pretokenise\0jitDebug\0"
34+
#define JSFLAG_NAMES "deepSleep\0unsafeFlash\0unsyncFiles\0pretokenise\0jitDebug\0noErrorSave\0"
3435
// NOTE: \0 also added by compiler - two \0's are required!
3536

3637
extern volatile JsFlags jsFlags;

src/jsinteractive.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,12 +954,20 @@ void jsiSemiInit(bool autoLoad, JsfFileName *loadedFilename) {
954954
#ifdef ESP8266
955955
jshPrintBanner();
956956
#endif
957+
#ifndef SAVE_ON_FLASH
958+
if (jsfFindFile(jsfNameFromString("ERROR"),NULL))
959+
jsiConsolePrint("\nAn Uncaught Error has been saved to Storage. Please type:\n"
960+
" require('Storage').read('ERROR') to view it\n"
961+
" require('Storage').erase('ERROR') to clear it\n");
962+
#endif
963+
957964
}
958965
#ifdef USE_TERMINAL
959966
if (consoleDevice != EV_TERMINAL) // don't spam the terminal
960967
#endif
961968
jsiConsolePrint("\n"); // output new line
962969
inputLineRemoved = true; // we need to put the input line back...
970+
963971
}
964972

965973
#ifdef BANGLEJS // On Bangle.js if Storage is corrupt, show a recovery menu
@@ -1289,13 +1297,31 @@ void jsiCheckErrors() {
12891297
jsiConsoleRemoveInputLine();
12901298
jsiConsolePrintf("Uncaught %v\n", exception);
12911299
reportedError = true;
1300+
#ifndef SAVE_ON_FLASH
1301+
JsVar *exceptionString = NULL;
1302+
if ((!jsfGetFlag(JSF_NO_ERRORS_SAVE)) && !jsfFindFile(jsfNameFromString("ERROR"),NULL))
1303+
exceptionString = jsvAsString(exception);
1304+
#endif
1305+
12921306
if (jsvIsObject(exception)) {
12931307
JsVar *stackTrace = jsvObjectGetChildIfExists(exception, "stack");
12941308
if (stackTrace) {
12951309
jsiConsolePrintStringVar(stackTrace);
1310+
#ifndef SAVE_ON_FLASH
1311+
if (exceptionString) {
1312+
jsvAppendCharacter(exceptionString, '\n');
1313+
jsvAppendStringVarComplete(exceptionString, stackTrace);
1314+
}
1315+
#endif
12961316
jsvUnLock(stackTrace);
12971317
}
12981318
}
1319+
#ifndef SAVE_ON_FLASH
1320+
if (exceptionString) {
1321+
jsfWriteFile(jsfNameFromString("ERROR"), exceptionString, JSFF_NONE, 0, 0);
1322+
jsvUnLock(exceptionString);
1323+
}
1324+
#endif
12991325
}
13001326
jsvUnLock(exception);
13011327
if (jspIsInterrupted()

src/jswrap_espruino.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,10 @@ code.
851851
* `unsyncFiles` - When writing files, *don't* flush all data to the SD card
852852
after each command (the default is *to* flush). This is much faster, but can
853853
cause filesystem damage if power is lost without the filesystem unmounted.
854+
* `jitDebug` - When JIT compiling, outputs debug info to the console
855+
* `noErrorSave` - [2v27+] when an uncaught error occurs, by default it is now
856+
written to a file called `ERROR` in Storage (the file is not updated). To
857+
stop this happening, use `E.setFlags({noErrorSave:true})`
854858
*/
855859
/*JSON{
856860
"type" : "staticmethod",

0 commit comments

Comments
 (0)