Skip to content

Commit 0c5fc03

Browse files
Alexey Poliakovclaude
andcommitted
fix: emit CODE_FAILED event on pairing code request failure
Wraps requestPairingCode in try/catch to emit code_failed event instead of crashing when pairing fails. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 7549ae4 commit 0c5fc03

1 file changed

Lines changed: 49 additions & 41 deletions

File tree

src/Client.js

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -595,49 +595,57 @@ class Client extends EventEmitter {
595595
showNotification = true,
596596
intervalMs = 180000,
597597
) {
598-
await exposeFunctionIfAbsent(
599-
this.pupPage,
600-
'onCodeReceivedEvent',
601-
async (code) => {
602-
this.emit(Events.CODE_RECEIVED, code);
603-
return code;
604-
},
605-
);
606-
return await this.pupPage.evaluate(
607-
async (phoneNumber, showNotification, intervalMs) => {
608-
const getCode = async () => {
609-
while (!window.AuthStore.PairingCodeLinkUtils) {
610-
await new Promise((resolve) =>
611-
setTimeout(resolve, 250),
598+
try {
599+
await exposeFunctionIfAbsent(
600+
this.pupPage,
601+
'onCodeReceivedEvent',
602+
async (code) => {
603+
this.emit(Events.CODE_RECEIVED, code);
604+
return code;
605+
},
606+
);
607+
return await this.pupPage.evaluate(
608+
async (phoneNumber, showNotification, intervalMs) => {
609+
const getCode = async () => {
610+
while (!window.AuthStore.PairingCodeLinkUtils) {
611+
await new Promise((resolve) =>
612+
setTimeout(resolve, 250),
613+
);
614+
}
615+
window.AuthStore.PairingCodeLinkUtils.setPairingType(
616+
'ALT_DEVICE_LINKING',
612617
);
618+
await window.AuthStore.PairingCodeLinkUtils.initializeAltDeviceLinking();
619+
return window.AuthStore.PairingCodeLinkUtils.startAltLinkingFlow(
620+
phoneNumber,
621+
showNotification,
622+
);
623+
};
624+
if (window.codeInterval) {
625+
clearInterval(window.codeInterval); // remove existing interval
613626
}
614-
window.AuthStore.PairingCodeLinkUtils.setPairingType(
615-
'ALT_DEVICE_LINKING',
616-
);
617-
await window.AuthStore.PairingCodeLinkUtils.initializeAltDeviceLinking();
618-
return window.AuthStore.PairingCodeLinkUtils.startAltLinkingFlow(
619-
phoneNumber,
620-
showNotification,
621-
);
622-
};
623-
if (window.codeInterval) {
624-
clearInterval(window.codeInterval); // remove existing interval
625-
}
626-
window.codeInterval = setInterval(async () => {
627-
const state =
628-
window.require('WAWebSocketModel').Socket.state;
629-
if (state != 'UNPAIRED' && state != 'UNPAIRED_IDLE') {
630-
clearInterval(window.codeInterval);
631-
return;
632-
}
633-
window.onCodeReceivedEvent(await getCode());
634-
}, intervalMs);
635-
return window.onCodeReceivedEvent(await getCode());
636-
},
637-
phoneNumber,
638-
showNotification,
639-
intervalMs,
640-
);
627+
window.codeInterval = setInterval(async () => {
628+
const state =
629+
window.require('WAWebSocketModel').Socket.state;
630+
if (state != 'UNPAIRED' && state != 'UNPAIRED_IDLE') {
631+
clearInterval(window.codeInterval);
632+
return;
633+
}
634+
window.onCodeReceivedEvent(await getCode());
635+
}, intervalMs);
636+
return window.onCodeReceivedEvent(await getCode());
637+
},
638+
phoneNumber,
639+
showNotification,
640+
intervalMs,
641+
);
642+
} catch (error) {
643+
/**
644+
* Emitted when the pairing code request fails
645+
* @event Client#code_failed
646+
*/
647+
this.emit(Events.CODE_FAILED);
648+
}
641649
}
642650

643651
/**

0 commit comments

Comments
 (0)