await error reporter (#3779)
This commit is contained in:
parent
af23bdd515
commit
192c4bcfd6
2 changed files with 25 additions and 10 deletions
5
.changeset/plenty-paws-divide.md
Normal file
5
.changeset/plenty-paws-divide.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix an issue with throwAndExit not awaiting
|
|
@ -8,7 +8,7 @@ import build from '../core/build/index.js';
|
||||||
import { openConfig } from '../core/config.js';
|
import { openConfig } from '../core/config.js';
|
||||||
import devServer from '../core/dev/index.js';
|
import devServer from '../core/dev/index.js';
|
||||||
import { collectErrorMetadata } from '../core/errors.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 { enableVerboseLogging, nodeLogDestination } from '../core/logger/node.js';
|
||||||
import { formatConfigErrorMessage, formatErrorMessage, printHelp } from '../core/messages.js';
|
import { formatConfigErrorMessage, formatErrorMessage, printHelp } from '../core/messages.js';
|
||||||
import preview from '../core/preview/index.js';
|
import preview from '../core/preview/index.js';
|
||||||
|
@ -170,24 +170,34 @@ export async function cli(args: string[]) {
|
||||||
try {
|
try {
|
||||||
await runCommand(cmd, flags);
|
await runCommand(cmd, flags);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return throwAndExit(cmd, err);
|
await throwAndExit(cmd, err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Display error and exit */
|
/** Display error and exit */
|
||||||
function throwAndExit(cmd: string, err: unknown) {
|
async function throwAndExit(cmd: string, err: unknown) {
|
||||||
let telemetryPromise: Promise<any>;
|
let telemetryPromise: Promise<any>;
|
||||||
|
let errorMessage: string;
|
||||||
|
function exitWithErrorMessage() {
|
||||||
|
console.error(errorMessage);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
if (err instanceof z.ZodError) {
|
if (err instanceof z.ZodError) {
|
||||||
console.error(formatConfigErrorMessage(err));
|
|
||||||
telemetryPromise = telemetry.record(eventConfigError({ cmd, err, isFatal: true }));
|
telemetryPromise = telemetry.record(eventConfigError({ cmd, err, isFatal: true }));
|
||||||
|
errorMessage = formatConfigErrorMessage(err);
|
||||||
} else {
|
} else {
|
||||||
const errorWithMetadata = collectErrorMetadata(createSafeError(err));
|
const errorWithMetadata = collectErrorMetadata(createSafeError(err));
|
||||||
console.error(formatErrorMessage(errorWithMetadata));
|
|
||||||
telemetryPromise = telemetry.record(eventError({ cmd, err: errorWithMetadata, isFatal: true }));
|
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));
|
// Timeout the error reporter (very short) because the user is waiting.
|
||||||
// Don't wait too long. Timeout the request faster than usual because the user is waiting.
|
// NOTE(fks): It is better that we miss some events vs. holding too long.
|
||||||
// TODO: Investigate using an AbortController once we drop Node v14 support.
|
// TODO(fks): Investigate using an AbortController once we drop Node v14.
|
||||||
setTimeout(() => process.exit(1), 300);
|
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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue