Skip to content

Commit

Permalink
improve error handling for debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyoshiaki committed Nov 9, 2023
1 parent 2ab88a4 commit 0cf78e9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 33 deletions.
40 changes: 23 additions & 17 deletions packages/ice/src/stun/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -118,6 +122,8 @@ export class StunProtocol implements Protocol {

try {
return await transaction.run();
} catch (e) {
throw e;
} finally {
delete this.transactions[request.transactionIdHex];
}
Expand Down
9 changes: 9 additions & 0 deletions packages/ice/src/stun/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
40 changes: 24 additions & 16 deletions packages/ice/src/turn/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand All @@ -63,6 +67,8 @@ class TurnTransport implements Protocol {

try {
return await transaction.run();
} catch (e) {
throw e;
} finally {
delete this.turn.transactions[request.transactionIdHex];
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -235,6 +241,8 @@ class TurnClient implements Protocol {

try {
return await transaction.run();
} catch (e) {
throw e;
} finally {
delete this.transactions[request.transactionIdHex];
}
Expand Down Expand Up @@ -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");
}
}

Expand Down

0 comments on commit 0cf78e9

Please sign in to comment.