Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getPressure: handle error when we don't have an exception #2438

Merged
merged 2 commits into from
Dec 19, 2023

Conversation

bobrippling
Copy link
Contributor

This handles an I2C error, originally ocurring in #2137, found again by chance in an unrelated PR.

When we create a promise for getPressure, we now look at both jspGetException and then jspHasError (separately).
If the former is null but we have an error, we raise an exception about the I2C communication with the barometer.

Previous behaviour would raise an exception with no payload, leading to the Uncaught Error: Unhandled promise rejection: undefined seen in the BangleApps pull request above.

Comment on lines +5087 to +5089
JsVar *msg = jsvNewFromString("I2C barometer error");
JsVar *exception = jswrap_internalerror_constructor(msg);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe I have the locking correct here - jswrap_internalerror_constructor unlocks msg, but would appreciate a second pair of eyes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think jswrap_internalerror_constructor does unlock msg - if it's a function called from JS (which I'm pretty sure it is as it's the constructor) then the caller is responsible for unlocking

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might've misread the code - I think here it unlocks in jsvObjectSetChildAndUnLock

JsVar *_jswrap_error_constructor(JsVar *msg, char *type) {
JsVar *d = jspNewObject(0,type);
if (!d) return 0;
if (msg) {
msg = jsvAsString(msg);
jsvObjectSetChildAndUnLock(d, "message", msg);
}
jsvObjectSetChildAndUnLock(d, "type", jsvNewFromString(type));
return d;
}

(⬆️ msg here)

Espruino/src/jsvar.c

Lines 3195 to 3198 in deedeaf

void jsvObjectSetChildAndUnLock(JsVar *obj, const char *name, JsVar *child) {
jsvObjectSetChild(obj, name, child);
jsvUnLock(child);
}

(⬆️ child here)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh - jsvObjectSetChildAndUnLock(d, "message", msg); does unlock msg but on the line above, msg = jsvAsString(msg); replaces msg with a newly locked version of msg that's guaranteed to be a string. I guess for readability it should really be written to a new variable instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense - I could make that change as part of this PR if you like?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's ok - i'll do it separately now...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but you still need to unlock msg...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes - I was assuming jsvAsString unlocked the value it was given, with us replacing it. Thanks for the pointer!

@gfwilliams
Copy link
Member

gfwilliams commented Dec 6, 2023

Looks like there's a build error here as well? https://github.com/espruino/Espruino/actions/runs/7112131428/job/19361472245?pr=2438

edit: ahh - you made a comment about this above

@bobrippling bobrippling force-pushed the fix/pressure-exceptions branch from deedeaf to a1bc1f9 Compare December 11, 2023 21:11
@bobrippling
Copy link
Contributor Author

This is ready for another look btw :)

@gfwilliams
Copy link
Member

Thanks - just looked again and this looks good! Merging now!

@gfwilliams gfwilliams merged commit ee8bc67 into espruino:master Dec 19, 2023
15 checks passed
@bobrippling bobrippling deleted the fix/pressure-exceptions branch December 19, 2023 21:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants