-
Notifications
You must be signed in to change notification settings - Fork 3
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
Add some thrown errors if the at driver has closed #57
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,14 @@ export class ATDriver { | |
} | ||
) | ||
); | ||
this.closed = new Promise(resolve => socket.once('close', () => resolve())); | ||
this.hasClosed = false; | ||
this.closed = new Promise(resolve => | ||
socket.once('close', () => { | ||
this.hasClosed = true; | ||
this.log(AgentMessage.AT_DRIVER_COMMS, { direction: 'closed' }); | ||
resolve(); | ||
}) | ||
); | ||
this._nextId = 0; | ||
} | ||
|
||
|
@@ -62,6 +69,7 @@ export class ATDriver { | |
for await (const rawMessage of iterateEmitter(this.socket, 'message', 'close', 'error')) { | ||
const message = rawMessage.toString(); | ||
this.log(AgentMessage.AT_DRIVER_COMMS, { direction: 'inbound', message }); | ||
if (this.hasClosed) throw new Error('AT-Driver connection unexpectedly closed'); | ||
yield JSON.parse(message); | ||
} | ||
} | ||
|
@@ -70,8 +78,10 @@ export class ATDriver { | |
const id = this._nextId++; | ||
const rawMessage = JSON.stringify({ id, ...command }); | ||
this.log(AgentMessage.AT_DRIVER_COMMS, { direction: 'outbound', message: rawMessage }); | ||
if (this.hasClosed) throw new Error('AT-Driver connection unexpectedly closed'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The call to |
||
this.socket.send(rawMessage); | ||
for await (const message of this._messages()) { | ||
if (this.hasClosed) throw new Error('AT-Driver connection unexpectedly closed'); | ||
if (message.id === id) { | ||
if (message.error) { | ||
throw new Error(message.error); | ||
|
@@ -101,6 +111,7 @@ export class ATDriver { | |
*/ | ||
async *speeches() { | ||
for await (const message of this._messages()) { | ||
if (this.hasClosed) throw new Error('AT-Driver connection unexpectedly closed'); | ||
if (message.method === 'interaction.capturedOutput') { | ||
yield message.params.data; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The design of
iterateEmitter
suggests that this is not an exceptional circumstance--the utility seems designed to gracefully handle the case where thecompletEvent
(which is"close"
in this case) occurs while values are still being emitted.I honestly don't have any recollection of the thinking that went into this, but from reviewing the source, I believe it may be because values are always emitted asynchronously. It seems possible that
close
occurs while the utility is still working through its internal backlog of values.If
"close"
is an exceptional circumstance in this context, then maybe we should specifynull
as thecompleteEvent
for this invocation.What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hrm, you seem right, 'close' and 'error' are called out here, going to spend a little more time looking one place above these calls to handled unexpected error rather than this particular throw
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thought this one through a lot and figured out where the throw needs to happen.
The
iterateEmitter
will properly throw if the connection is closed while it's iterating, but if it were to call into this_messages
while starting in a closed state, it would never throw again. Therefore I moved the check for an already closed socket to the top of _messages