Skip to content

Commit ab55fee

Browse files
committed
fix: exit with error code on API error
for instance, when the CI token is invalid or has expired.
1 parent 6b49654 commit ab55fee

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/APIClient.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export class APIClient {
2828

2929
onConnected?: (helloMessage: APIHello) => void;
3030
onEvent?: (event: APIEvent) => void;
31+
onError?: (error: APIError) => void;
3132

3233
constructor(readonly token: string, readonly server = DEFAULT_SERVER) {
3334
this.socket = this.createSocket(token, server);
@@ -160,7 +161,14 @@ export class APIClient {
160161
processMessage(message: APIError | APIHello | APIEvent | APIResponse) {
161162
switch (message.type) {
162163
case 'error':
164+
if (this.onError) {
165+
this.onError(message);
166+
}
163167
console.error('API Error:', message.message);
168+
if (this.pendingCommands.size > 0) {
169+
const [, reject] = this.pendingCommands.values().next().value;
170+
reject(new Error(message.message));
171+
}
164172
break;
165173

166174
case 'hello':

src/main.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,17 @@ async function main() {
156156
}
157157

158158
const client = new APIClient(token);
159+
client.onConnected = (hello) => {
160+
if (!quiet) {
161+
console.log(`Connected to Wokwi Simulation API ${hello.appVersion}`);
162+
}
163+
};
164+
client.onError = (error) => {
165+
console.error('API Error:', error.message);
166+
process.exit(1);
167+
};
168+
159169
try {
160-
client.onConnected = (hello) => {
161-
if (!quiet) {
162-
console.log(`Connected to Wokwi Simulation API ${hello.appVersion}`);
163-
}
164-
};
165170
await client.connected;
166171
await client.fileUpload('diagram.json', diagram);
167172
const extension = firmwarePath.split('.').pop();

0 commit comments

Comments
 (0)