Skip to content

Commit

Permalink
await error reporter (#3779)
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott authored Jun 30, 2022
1 parent af23bdd commit 192c4bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-paws-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix an issue with throwAndExit not awaiting
30 changes: 20 additions & 10 deletions packages/astro/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import build from '../core/build/index.js';
import { openConfig } from '../core/config.js';
import devServer from '../core/dev/index.js';
import { collectErrorMetadata } from '../core/errors.js';
import { LogOptions } from '../core/logger/core.js';
import { debug, LogOptions } from '../core/logger/core.js';
import { enableVerboseLogging, nodeLogDestination } from '../core/logger/node.js';
import { formatConfigErrorMessage, formatErrorMessage, printHelp } from '../core/messages.js';
import preview from '../core/preview/index.js';
Expand Down Expand Up @@ -170,24 +170,34 @@ export async function cli(args: string[]) {
try {
await runCommand(cmd, flags);
} catch (err) {
return throwAndExit(cmd, err);
await throwAndExit(cmd, err);
}
}

/** Display error and exit */
function throwAndExit(cmd: string, err: unknown) {
async function throwAndExit(cmd: string, err: unknown) {
let telemetryPromise: Promise<any>;
let errorMessage: string;
function exitWithErrorMessage() {
console.error(errorMessage);
process.exit(1);
}

if (err instanceof z.ZodError) {
console.error(formatConfigErrorMessage(err));
telemetryPromise = telemetry.record(eventConfigError({ cmd, err, isFatal: true }));
errorMessage = formatConfigErrorMessage(err);
} else {
const errorWithMetadata = collectErrorMetadata(createSafeError(err));
console.error(formatErrorMessage(errorWithMetadata));
telemetryPromise = telemetry.record(eventError({ cmd, err: errorWithMetadata, isFatal: true }));
errorMessage = formatErrorMessage(errorWithMetadata);
}
// Wait for the telemetry event to send, then exit. Ignore an error.
telemetryPromise.catch(() => undefined).then(() => process.exit(1));
// Don't wait too long. Timeout the request faster than usual because the user is waiting.
// TODO: Investigate using an AbortController once we drop Node v14 support.
setTimeout(() => process.exit(1), 300);

// Timeout the error reporter (very short) because the user is waiting.
// NOTE(fks): It is better that we miss some events vs. holding too long.
// TODO(fks): Investigate using an AbortController once we drop Node v14.
setTimeout(exitWithErrorMessage, 400);
// Wait for the telemetry event to send, then exit. Ignore any error.
await telemetryPromise
.catch((err) => debug('telemetry', `record() error: ${err.message}`))
.then(exitWithErrorMessage);
}

0 comments on commit 192c4bc

Please sign in to comment.