Skip to content

Commit

Permalink
Tidying up error messages - we've now saved 1.5k on Micrbit 1!
Browse files Browse the repository at this point in the history
  • Loading branch information
gfwilliams committed Oct 6, 2023
1 parent 4327ebd commit 7448823
Show file tree
Hide file tree
Showing 29 changed files with 111 additions and 103 deletions.
4 changes: 2 additions & 2 deletions libs/bluetooth/bluetooth_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const char *bleVarToUUID(ble_uuid_t *uuid, JsVar *v) {
uuid->uuid = i;
return 0;
}
if (!jsvIsString(v)) return "UUID Not a String or Integer";
if (!jsvIsString(v)) return "UUID not String or Integer";
unsigned int expectedLength = 16;
unsigned int startIdx = 0;
if (jsvIsStringEqualOrStartsWith(v,"0x",true)) {
Expand Down Expand Up @@ -185,7 +185,7 @@ const char *bleVarToUUID(ble_uuid_t *uuid, JsVar *v) {
data[13] = 0;
err_code = sd_ble_uuid_vs_add((ble_uuid128_t*)data, &uuid->type);
if (err_code == NRF_ERROR_NO_MEM)
return "Too many custom UUIDs already";
return "Too many custom UUIDs";
}
return err_code ? "BLE device error adding UUID" : 0;
#else
Expand Down
24 changes: 12 additions & 12 deletions libs/bluetooth/jswrap_bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ void jswrap_ble_setAddress(JsVar *address) {
#ifdef NRF52_SERIES
ble_gap_addr_t p_addr;
if (!bleVarToAddr(address, &p_addr)) {
jsExceptionHere(JSET_ERROR, "Expecting a mac address of the form aa:bb:cc:dd:ee:ff");
jsExceptionHere(JSET_ERROR, "Expecting mac address of the form aa:bb:cc:dd:ee:ff");
return;
}
jsvObjectSetChild(execInfo.hiddenRoot, BLE_NAME_MAC_ADDRESS, address);
Expand Down Expand Up @@ -1043,7 +1043,7 @@ void jswrap_ble_setAdvertising(JsVar *data, JsVar *options) {
jsvUnLock(v);
}
} else if (!jsvIsUndefined(options)) {
jsExceptionHere(JSET_TYPEERROR, "Expecting 'options' to be object or undefined, got %t", options);
jsExceptionHere(JSET_TYPEERROR, "Expecting Object or undefined, got %t", options);
return;
}

Expand Down Expand Up @@ -1197,7 +1197,7 @@ JsVar *jswrap_ble_getAdvertisingData(JsVar *data, JsVar *options) {
}
#endif
} else if (!jsvIsUndefined(options)) {
jsExceptionHere(JSET_TYPEERROR, "Expecting 'options' to be object or undefined, got %t", options);
jsExceptionHere(JSET_TYPEERROR, "Expecting Object or undefined, got %t", options);
return 0;
}

Expand Down Expand Up @@ -1247,7 +1247,7 @@ JsVar *jswrap_ble_getAdvertisingData(JsVar *data, JsVar *options) {
advdata.uuids_complete.p_uuids = adv_uuid;
#endif
} else if (!jsvIsUndefined(data)) {
jsExceptionHere(JSET_TYPEERROR, "Expecting object, array or undefined, got %t", data);
jsExceptionHere(JSET_TYPEERROR, "Expecting Object, Array or undefined, got %t", data);
return 0;
}

Expand Down Expand Up @@ -1472,7 +1472,7 @@ mitm. See `NRF.setSecurity` for more information.
*/
void jswrap_ble_setServices(JsVar *data, JsVar *options) {
if (!(jsvIsObject(data) || jsvIsUndefined(data))) {
jsExceptionHere(JSET_TYPEERROR, "Expecting object or undefined, got %t", data);
jsExceptionHere(JSET_TYPEERROR, "Expecting Object or undefined, got %t", data);
return;
}

Expand Down Expand Up @@ -1765,7 +1765,7 @@ void jswrap_ble_updateServices(JsVar *data) {
jsvObjectIteratorFree(&it);

} else if (!jsvIsUndefined(data)) {
jsExceptionHere(JSET_TYPEERROR, "Expecting object or undefined, got %t", data);
jsExceptionHere(JSET_TYPEERROR, "Expecting Object or undefined, got %t", data);
}
}

Expand Down Expand Up @@ -2064,7 +2064,7 @@ void jswrap_ble_setScan(JsVar *callback, JsVar *options) {
return;
}
} else if (options)
jsExceptionHere(JSET_TYPEERROR, "Expecting Object got %t", options);
jsExceptionHere(JSET_TYPEERROR, "Expecting Object, got %t", options);
// set the callback event variable
if (!jsvIsFunction(callback)) callback=0;
if (callback) {
Expand Down Expand Up @@ -2463,7 +2463,7 @@ void jswrap_nfc_URL(JsVar *url) {
}

if (!jsvIsString(url)) {
jsExceptionHere(JSET_TYPEERROR, "Expecting a String, got %t", url);
jsExceptionHere(JSET_TYPEERROR, "Expecting String, got %t", url);
return;
}

Expand Down Expand Up @@ -2796,7 +2796,7 @@ void jswrap_ble_sendHIDReport(JsVar *data, JsVar *callback) {
jsvObjectSetChild(execInfo.root, BLE_HID_SENT_EVENT, callback);
jsble_send_hid_input_report((uint8_t*)vPtr, vLen);
} else {
jsExceptionHere(JSET_ERROR, "Expecting array, got %t", data);
jsExceptionHere(JSET_ERROR, "Expecting Array, got %t", data);
}
#endif
}
Expand Down Expand Up @@ -3375,7 +3375,7 @@ void jswrap_ble_requestDevice_scan(JsVar *device) {
JsVar *jswrap_ble_requestDevice(JsVar *options) {
#if CENTRAL_LINK_COUNT>0
if (!(jsvIsUndefined(options) || jsvIsObject(options))) {
jsExceptionHere(JSET_TYPEERROR, "Expecting an object, for %t", options);
jsExceptionHere(JSET_TYPEERROR, "Expecting Object, got %t", options);
return 0;
}
JsVar *filters = jsvObjectGetChildIfExists(options, "filters");
Expand Down Expand Up @@ -3667,7 +3667,7 @@ open.
*/
void jswrap_ble_setSecurity(JsVar *options) {
if (!jsvIsObject(options) && !jsvIsUndefined(options))
jsExceptionHere(JSET_TYPEERROR, "Expecting an object or undefined, got %t", options);
jsExceptionHere(JSET_TYPEERROR, "Expecting Object or undefined, got %t", options);
else {
jsvObjectSetOrRemoveChild(execInfo.hiddenRoot, BLE_NAME_SECURITY, options);
jsble_update_security();
Expand Down Expand Up @@ -3940,7 +3940,7 @@ JsVar *jswrap_ble_BluetoothRemoteGATTServer_connect(JsVar *parent, JsVar *option
ble_gap_addr_t peer_addr;
if (!bleVarToAddr(addr, &peer_addr)) {
jsvUnLock2(device, addr);
jsExceptionHere(JSET_TYPEERROR, "Expecting a device with a mac address of the form aa:bb:cc:dd:ee:ff");
jsExceptionHere(JSET_TYPEERROR, "Expecting device with a mac address of the form aa:bb:cc:dd:ee:ff");
return 0;
}
jsvUnLock(device);
Expand Down
2 changes: 1 addition & 1 deletion libs/graphics/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ bool graphicsSetCallbacks(JsGraphics *gfx) {
lcd_spi_unbuf_setCallbacks(gfx);
#endif
} else {
jsExceptionHere(JSET_INTERNALERROR, "Unknown graphics type");
// this should never happen
assert(0);
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions libs/graphics/jswrap_graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ bool _jswrap_graphics_parseImage(JsGraphics *gfx, JsVar *image, unsigned int ima
}
jsvStringIteratorFree(&it);
} else {
jsExceptionHere(JSET_ERROR, "Expecting first argument to be an object or a String");
jsExceptionHere(JSET_ERROR, "First argument must be Object or String");
return 0;
}
if (!info->isTransparent)
Expand Down Expand Up @@ -275,7 +275,7 @@ bool _jswrap_graphics_parseImage(JsGraphics *gfx, JsVar *image, unsigned int ima
info->width<=0 ||
info->height<=0 ||
info->bpp>32) {
jsExceptionHere(JSET_ERROR, "Expecting first argument to a valid Image");
jsExceptionHere(JSET_ERROR, "Expecting valid Image");
_jswrap_graphics_freeImageInfo(info);
return false;
}
Expand Down Expand Up @@ -822,7 +822,7 @@ be ignored. Spaces are treated as `0`, and any other character is a `1`
*/
JsVar *jswrap_graphics_createImage(JsVar *data) {
if (!jsvIsString(data)) {
jsExceptionHere(JSET_TYPEERROR, "Expecting a String");
jsExceptionHere(JSET_TYPEERROR, "Expecting String");
return 0;
}
int x=0,y=0;
Expand Down Expand Up @@ -3498,7 +3498,7 @@ JsVar *jswrap_graphics_drawImages(JsVar *parent, JsVar *layersVar, JsVar *option
GfxDrawImageLayer layers[MAXIMAGES];
int i,layerCount;
if (!(jsvIsArray(layersVar) && (layerCount=jsvGetArrayLength(layersVar))<=MAXIMAGES)) {
jsExceptionHere(JSET_TYPEERROR,"Expecting array for first argument with <%d entries", MAXIMAGES);
jsExceptionHere(JSET_TYPEERROR,"Expecting Array for first argument with <%d entries", MAXIMAGES);
return 0;
}
// default bounds (=nothing)
Expand Down
2 changes: 1 addition & 1 deletion libs/microbit/jswrap_microbit.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ void jswrap_microbit_show(JsVar *image) {
} else if (jsvIsNumeric(image)) {
newState = jsvGetInteger(image);
} else {
jsError("Expecting a number, got %t\n", image);
jsError("Expecting Number, got %t\n", image);
return;
}
jswrap_microbit_show_raw(newState);
Expand Down
2 changes: 1 addition & 1 deletion libs/network/esp32/jswrap_esp32_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static system_event_sta_disconnected_t g_lastEventStaDisconnected;
static bool g_isStaConnected = false;

#define EXPECT_CB_EXCEPTION(jsCB) jsExceptionHere(JSET_ERROR, "Expecting callback function but got %v", jsCB)
#define EXPECT_OPT_EXCEPTION(jsOPT) jsExceptionHere(JSET_ERROR, "Expecting options object but got %t", jsOPT)
#define EXPECT_OPT_EXCEPTION(jsOPT) jsExceptionHere(JSET_ERROR, "Expecting Object, got %t", jsOPT)


//===== mDNS
Expand Down
2 changes: 1 addition & 1 deletion libs/network/esp8266/jswrap_esp8266_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ void jswrap_wifi_connect(
// Check for jsOptions
if (jsOptions != NULL) {
if (!jsvIsObject(jsOptions)) {
jsExceptionHere(JSET_ERROR, "Expecting an Object");
jsExceptionHere(JSET_ERROR, "Expecting Object, got %t", jsOptions);
return;
} else {

Expand Down
2 changes: 1 addition & 1 deletion libs/network/socketserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ int socketSendData(JsNetwork *net, JsVar *connection, int sckt, JsVar **sendData
if ((socketType&ST_TYPE_MASK)==ST_UDP) {
sndBufLen = (size_t)jsvGetStringLength(*sendData);
if (sndBufLen+1024 > jsuGetFreeStack()) {
jsExceptionHere(JSET_ERROR, "Not enough free stack to send this amount of data");
jsExceptionHere(JSET_ERROR, "Not enough stack memory for data");
return -1;
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion libs/network/telnet/jswrap_telnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ ESP8266 and on Linux **. It uses port 23 on the ESP8266 and port 2323 on Linux.
void jswrap_telnet_setOptions(JsVar *jsOptions) {
// Make sure jsOptions is an object
if (!jsvIsObject(jsOptions)) {
jsExceptionHere(JSET_ERROR, "Expecting options object but got %t", jsOptions);
jsExceptionHere(JSET_ERROR, "Expecting Object, got %t", jsOptions);
return;
}

Expand Down
6 changes: 3 additions & 3 deletions libs/puckjs/jswrap_puck.c
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ accelerometer at sample rates above 208Hz.
void puck_accelHandler(bool state, IOEventFlags flags) {
jshHadEvent(); // ensure we leave sleep to go to the idle loop
/* By having this function defined, we ensure that we don't push
the event onto the IO event queue which would cause Espruino a
the event onto the IO event queue which would cause Espruino a
little extra processing each time */
}

Expand Down Expand Up @@ -1024,7 +1024,7 @@ void jswrap_puck_accelOff() {
return;
}
if (accel_enabled) {
if (puckAccelChannel!=EV_NONE)
if (puckAccelChannel!=EV_NONE)
jshSetEventCallback(puckAccelChannel, NULL);
puckAccelChannel = EV_NONE;
jshPinWatch(ACCEL_PIN_INT, false, JSPW_NONE);
Expand Down Expand Up @@ -1183,7 +1183,7 @@ void jswrap_puck_IR(JsVar *data, Pin cathode, Pin anode) {
return;
}
if (!jsvIsIterable(data)) {
jsExceptionHere(JSET_TYPEERROR, "Expecting an array, got %t", data);
jsExceptionHere(JSET_TYPEERROR, "Expecting Array, got %t", data);
return;
}
if (jshIsPinValid(anode) && !jshIsPinValid(cathode)) {
Expand Down
2 changes: 1 addition & 1 deletion libs/tv/jswrap_tv.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ See the [Television](/Television) page for more information.

JsVar *jswrap_tv_setup(JsVar *options) {
if (!jsvIsObject(options)) {
jsExceptionHere(JSET_ERROR, "Expecting an options object, got %t", options);
jsExceptionHere(JSET_ERROR, "Expecting Object, got %t", options);
return 0;
}
JsVar *tvType = jsvObjectGetChildIfExists(options, "type");
Expand Down
14 changes: 8 additions & 6 deletions src/jsparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ bool jspeiAddScope(JsVar *scope) {

void jspeiRemoveScope() {
if (!execInfo.scopesVar || !jsvGetArrayLength(execInfo.scopesVar)) {
jsExceptionHere(JSET_INTERNALERROR, "Too many scopes removed");
jspSetError(false);
// This should never happen unless there's an interpreter error - no need to have an error message
assert(0);
//jsExceptionHere(JSET_INTERNALERROR, "Too many scopes removed");
//jspSetError(false);
return;
}
jsvUnLock(jsvArrayPop(execInfo.scopesVar));
Expand Down Expand Up @@ -542,7 +544,7 @@ NO_INLINE JsVar *jspeFunctionCall(JsVar *function, JsVar *functionName, JsVar *t
JsVar *returnVar = 0;

if (!jsvIsFunction(function)) {
jsExceptionHere(JSET_ERROR, "Expecting a function to call, got %t", function);
jsExceptionHere(JSET_ERROR, "Expecting function, got %t", function);
return 0;
}
JsVar *thisVar = jsvLockAgainSafe(thisArg);
Expand Down Expand Up @@ -1836,7 +1838,7 @@ NO_INLINE JsVar *jspeFactor() {
} else if (lex->tk==LEX_REGEX) {
JsVar *a = 0;
#ifdef ESPR_NO_REGEX
jsExceptionHere(JSET_SYNTAXERROR, "RegEx are not supported in this version of Espruino");
jsExceptionHere(JSET_SYNTAXERROR, "RegEx not supported in this build");
#else
if (JSP_SHOULD_EXECUTE) {
JsVar *regex = jslGetTokenValueAsVar();
Expand Down Expand Up @@ -2075,7 +2077,7 @@ NO_INLINE JsVar *__jspeBinaryExpression(JsVar *a, unsigned int lastPrecedence) {
JsVar *av = jsvSkipName(a);
JsVar *bv = jsvSkipName(b);
if (!jsvIsFunction(bv)) {
jsExceptionHere(JSET_ERROR, "Expecting a function on RHS in instanceof check, got %t", bv);
jsExceptionHere(JSET_ERROR, "Expecting function on RHS, got %t", bv);
} else {
if (jsvIsObject(av) || jsvIsFunction(av)) {
JsVar *bproto = jspGetNamedField(bv, JSPARSE_PROTOTYPE_VAR, false);
Expand Down Expand Up @@ -2491,7 +2493,7 @@ NO_INLINE JsVar *jspeStatementSwitch() {
JSP_RESTORE_EXECUTE();
}
if (lex->tk==LEX_R_CASE) {
jsExceptionHere(JSET_SYNTAXERROR, "Espruino doesn't support CASE after DEFAULT");
jsExceptionHere(JSET_SYNTAXERROR, "CASE after DEFAULT unsupported");
return 0;
}
JSP_MATCH('}');
Expand Down
2 changes: 1 addition & 1 deletion src/jsserial.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ bool jsserialPopulateUSARTInfo(
if (!jsvIsUndefined(baud)) {
int b = (int)jsvGetInteger(baud);
if (b<=100 || b > 10000000)
jsExceptionHere(JSET_ERROR, "Invalid baud rate specified");
jsExceptionHere(JSET_ERROR, "Invalid baud rate %d", b);
else
inf->baudRate = b;
}
Expand Down
10 changes: 5 additions & 5 deletions src/jsvar.c
Original file line number Diff line number Diff line change
Expand Up @@ -2131,7 +2131,7 @@ JsVarInt jsvGetInteger(const JsVar *v) {
if (jsvIsString(v) && jsvIsStringNumericInt(v, true/* allow decimal point*/)) {
char buf[32];
if (jsvGetString(v, buf, sizeof(buf))==sizeof(buf))
jsExceptionHere(JSET_ERROR, "String too big to convert to integer");
jsExceptionHere(JSET_ERROR, "String too big to convert to number");
else
return (JsVarInt)stringToInt(buf);
}
Expand Down Expand Up @@ -2197,7 +2197,7 @@ JsVarFloat jsvGetFloat(const JsVar *v) {
if (jsvIsString(v)) {
char buf[64];
if (jsvGetString(v, buf, sizeof(buf))==sizeof(buf)) {
jsExceptionHere(JSET_ERROR, "String too big to convert to float");
jsExceptionHere(JSET_ERROR, "String too big to convert to number");
} else {
if (buf[0]==0) return 0; // empty string -> 0
if (!strcmp(buf,"Infinity")) return INFINITY;
Expand All @@ -2223,7 +2223,7 @@ JsVar *jsvAsNumber(JsVar *var) {
// handle strings like this, in case they're too big for an int
char buf[64];
if (jsvGetString(var, buf, sizeof(buf))==sizeof(buf)) {
jsExceptionHere(JSET_ERROR, "String too big to convert to integer");
jsExceptionHere(JSET_ERROR, "String too big to convert to number");
return jsvNewFromFloat(NAN);
} else
return jsvNewFromLongInteger(stringToInt(buf));
Expand Down Expand Up @@ -2331,7 +2331,7 @@ void jsvReplaceWith(JsVar *dst, JsVar *src) {
// if we can't find a char in a string we still return a NewChild,
// but we can't add character back in
if (!jsvHasChildren(parent)) {
jsExceptionHere(JSET_ERROR, "Field or method \"%v\" does not already exist, and can't create it on %t", dst, parent);
jsExceptionHere(JSET_ERROR, "Field or method %q does not already exist, and can't create it on %t", dst, parent);
} else {
// Remove the 'new child' flagging
jsvUnRef(parent);
Expand Down Expand Up @@ -4403,7 +4403,7 @@ JsvIsInternalChecker jsvGetInternalFunctionCheckerFor(JsVar *v) {
bool jsvReadConfigObject(JsVar *object, jsvConfigObject *configs, int nConfigs) {
if (jsvIsUndefined(object)) return true;
if (!jsvIsObject(object)) {
jsExceptionHere(JSET_ERROR, "Expecting an Object, or undefined");
jsExceptionHere(JSET_ERROR, "Expecting Object or undefined, got %t", object);
return false;
}
// Ok, it's an object
Expand Down
2 changes: 1 addition & 1 deletion src/jsvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ void jsvFree(void *ptr);
if (DATA && !TARGET_PTR) { \
TARGET_LENGTH = (size_t)jsvIterateCallbackCount(DATA); \
if (TARGET_LENGTH+256 > jsuGetFreeStack()) { \
jsExceptionHere(JSET_ERROR, "Not enough stack memory to decode data"); \
jsExceptionHere(JSET_ERROR, "Not enough stack memory for data"); \
} else { \
TARGET_PTR = (char *)alloca(TARGET_LENGTH); \
jsvIterateCallbackToBytes(DATA, (unsigned char *)TARGET_PTR, \
Expand Down
4 changes: 2 additions & 2 deletions src/jsvariterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ bool jsvIterateCallback(
}
jsvIteratorFree(&it);
} else {
jsExceptionHere(JSET_TYPEERROR, "Expecting a number or something iterable, got %t", data);
jsExceptionHere(JSET_TYPEERROR, "Expecting Number or iterable, got %t", data);
ok = false;
}
return ok;
Expand Down Expand Up @@ -197,7 +197,7 @@ bool jsvIterateBufferCallback(
}
jsvIteratorFree(&it);
} else {
jsExceptionHere(JSET_TYPEERROR, "Expecting a number or something iterable, got %t", data);
jsExceptionHere(JSET_TYPEERROR, "Expecting Number or iterable, got %t", data);
ok = false;
}
return ok;
Expand Down
Loading

0 comments on commit 7448823

Please sign in to comment.