This is probably me doing something completely wrong I am not an experienced JavaScript developer. I’m stuck here now and reaching out to the community to help me unstuck!
I have a custom view for pairing a new device. In this view, user can enter a House Code and a Unit Code and a Name for the unit. Regardless of outcome from Homey.createDevice()
the then()
is always called and the catch()
is never called.
I’m letting Homey API fail the call if I register two devices with the same data.id, this will yield a 409 conflict error. Am hoping to pick that error code up and display a nice message to the user and let him try again.
<script type="text/javascript">
(function () {
const $create = document.getElementById("receiver-add-create");
let creatingDevice = false;
$create.addEventListener("click", (e) => {
if (creatingDevice) return;
creatingDevice = true;
...
Homey.createDevice(device)
.then(function () {
return Homey.done();
})
.catch(function (err) {
creatingDevice = false;
return Homey.Alert(
"There is already a device with that House Code and Unit Code combination."
);
});
});
})();
</script>
EXPECTED: Display an alert if any error happens and if Home.createDevice()
is successful paring window will be closed.
ACTUAL: Regardless of outcome from Homey.createDevice then()
is called, catch()
is never called on errors, and if an error occurs Homey will display an error toast I cannot get rid of.
QUESTION: Am I going about this in the wrong way? If so, what should I be doing instead?