Skip to content

Commit

Permalink
Merge pull request #8 from tcavenezuela/feature/avoid-node-errors
Browse files Browse the repository at this point in the history
Change throw error to use the onException callback to avoid an unhand…
  • Loading branch information
carl0shd committed Jan 28, 2024
2 parents 96603fe + becea22 commit 38e4a63
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-chicken-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@simconnect.js/api': patch
---

Change throw error to use the onException callback to avoid an unhandle promise rejection.
11 changes: 8 additions & 3 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,27 @@ export class SIMCONNECT_API {
const remote = (this.remote = host ? { host, port: port ?? 500 } : undefined);
/* @ts-ignore */
const { handle, recvOpen } = await open(this.appName, Protocol.FSX_SP2, remote);
if (!handle) throw new Error(`No connection handle to the simulator.`);

if (!handle) {
opts.onException('No connection handle to the simulator.');
}

this.handle = handle;
this.connected = true;
this.simulatorName = recvOpen.applicationName;
handle.on('event', (event) => this.handleSystemEvent(event));
handle.on('close', () => opts.autoReconnect && this.connect(opts));
handle.on('exception', (e) => opts.onException(SIMCONNECT_EXCEPTION[e.exception]));

// Signal that we're done
opts.onConnect(handle, recvOpen);
} catch (err) {
if (opts.retries) {
opts.retries--;
opts.onRetry(opts.retries, opts.retryInterval);
setTimeout(() => this.connect(opts), 1000 * opts.retryInterval);
} else throw new Error(`No connection to the simulator.`);
} else {
opts.onException('No connection to the simulator.');
}
}
}

Expand Down

0 comments on commit 38e4a63

Please sign in to comment.