From 0cf78e968bfcee185500fda11f766f653a3d4579 Mon Sep 17 00:00:00 2001 From: shinyoshiaki Date: Thu, 9 Nov 2023 16:47:33 +0900 Subject: [PATCH] improve error handling for debugger --- packages/ice/src/stun/protocol.ts | 40 ++++++++++++++++------------ packages/ice/src/stun/transaction.ts | 9 +++++++ packages/ice/src/turn/protocol.ts | 40 +++++++++++++++++----------- 3 files changed, 56 insertions(+), 33 deletions(-) diff --git a/packages/ice/src/stun/protocol.ts b/packages/ice/src/stun/protocol.ts index e52473fa6..7a67fe545 100644 --- a/packages/ice/src/stun/protocol.ts +++ b/packages/ice/src/stun/protocol.ts @@ -55,23 +55,27 @@ export class StunProtocol implements Protocol { }; private datagramReceived(data: Buffer, addr: Address) { - if (!this.localCandidate) throw new Error("not exist"); - - const message = parseMessage(data); - if (!message) { - this.receiver.dataReceived(data, this.localCandidate.component); - return; - } - // log("parseMessage", addr, message); - if ( - (message.messageClass === classes.RESPONSE || - message.messageClass === classes.ERROR) && - this.transactionsKeys.includes(message.transactionIdHex) - ) { - const transaction = this.transactions[message.transactionIdHex]; - transaction.responseReceived(message, addr); - } else if (message.messageClass === classes.REQUEST) { - this.receiver.requestReceived(message, addr, this, data); + try { + if (!this.localCandidate) throw new Error("not exist"); + + const message = parseMessage(data); + if (!message) { + this.receiver.dataReceived(data, this.localCandidate.component); + return; + } + // log("parseMessage", addr, message); + if ( + (message.messageClass === classes.RESPONSE || + message.messageClass === classes.ERROR) && + this.transactionsKeys.includes(message.transactionIdHex) + ) { + const transaction = this.transactions[message.transactionIdHex]; + transaction.responseReceived(message, addr); + } else if (message.messageClass === classes.REQUEST) { + this.receiver.requestReceived(message, addr, this, data); + } + } catch (error) { + log("datagramReceived error", error); } } @@ -118,6 +122,8 @@ export class StunProtocol implements Protocol { try { return await transaction.run(); + } catch (e) { + throw e; } finally { delete this.transactions[request.transactionIdHex]; } diff --git a/packages/ice/src/stun/transaction.ts b/packages/ice/src/stun/transaction.ts index c4eb25299..19021f8f4 100644 --- a/packages/ice/src/stun/transaction.ts +++ b/packages/ice/src/stun/transaction.ts @@ -40,6 +40,15 @@ export class Transaction { try { this.retry(); return await this.onResponse.asPromise(); + } catch (error) { + log( + "transaction run failed", + error, + this.protocol.type, + this.request.toJSON() + ); + + throw error; } finally { if (this.timeoutHandle) { clearTimeout(this.timeoutHandle); diff --git a/packages/ice/src/turn/protocol.ts b/packages/ice/src/turn/protocol.ts index c853d3f0e..ccce781f2 100644 --- a/packages/ice/src/turn/protocol.ts +++ b/packages/ice/src/turn/protocol.ts @@ -31,21 +31,25 @@ class TurnTransport implements Protocol { } private datagramReceived = (data: Buffer, addr: Address) => { - const message = parseMessage(data); - if (!message) { - this.receiver?.dataReceived(data, this.localCandidate.component); - return; - } + try { + const message = parseMessage(data); + if (!message) { + this.receiver?.dataReceived(data, this.localCandidate.component); + return; + } - if ( - (message?.messageClass === classes.RESPONSE || - message?.messageClass === classes.ERROR) && - this.turn.transactions[message.transactionIdHex] - ) { - const transaction = this.turn.transactions[message.transactionIdHex]; - transaction.responseReceived(message, addr); - } else if (message?.messageClass === classes.REQUEST) { - this.receiver?.requestReceived(message, addr, this, data); + if ( + (message?.messageClass === classes.RESPONSE || + message?.messageClass === classes.ERROR) && + this.turn.transactions[message.transactionIdHex] + ) { + const transaction = this.turn.transactions[message.transactionIdHex]; + transaction.responseReceived(message, addr); + } else if (message?.messageClass === classes.REQUEST) { + this.receiver?.requestReceived(message, addr, this, data); + } + } catch (error) { + log("datagramReceived error", error); } }; @@ -63,6 +67,8 @@ class TurnTransport implements Protocol { try { return await transaction.run(); + } catch (e) { + throw e; } finally { delete this.turn.transactions[request.transactionIdHex]; } @@ -199,7 +205,7 @@ class TurnClient implements Protocol { } refresh = () => - new PCancelable(async (r, f, onCancel) => { + new PCancelable(async (_, f, onCancel) => { let run = true; onCancel(() => { run = false; @@ -235,6 +241,8 @@ class TurnClient implements Protocol { try { return await transaction.run(); + } catch (e) { + throw e; } finally { delete this.transactions[request.transactionIdHex]; } @@ -272,7 +280,7 @@ class TurnClient implements Protocol { .setAttribute("XOR-PEER-ADDRESS", addr); const [response] = await this.request(request, this.server); if (response.messageMethod !== methods.CHANNEL_BIND) { - throw new Error(); + throw new Error("should be CHANNEL_BIND"); } }