0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-27 22:19:04 -05:00

Improve astro sync error handling (#9888)

* fix(#9711): improve `astro sync` error handling

* Update .changeset/healthy-jokes-deny.md

* update unit tests

---------

Co-authored-by: Arsh <69170106+lilnasy@users.noreply.github.com>
This commit is contained in:
Nate Moore 2024-02-01 15:59:14 -06:00 committed by GitHub
parent 756f9595e7
commit 9d2fdb293d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 21 additions and 27 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Improves error handling logic for the `astro sync` command.

View file

@ -27,7 +27,7 @@ type ErrorTypes =
| 'AggregateError'; | 'AggregateError';
export function isAstroError(e: unknown): e is AstroError { export function isAstroError(e: unknown): e is AstroError {
return e instanceof Error && (e as AstroError).type === 'AstroError'; return e instanceof AstroError;
} }
export class AstroError extends Error { export class AstroError extends Error {

View file

@ -18,6 +18,8 @@ import { createVite } from '../create-vite.js';
import { AstroError, AstroErrorData, createSafeError, isAstroError } from '../errors/index.js'; import { AstroError, AstroErrorData, createSafeError, isAstroError } from '../errors/index.js';
import type { Logger } from '../logger/core.js'; import type { Logger } from '../logger/core.js';
import { ensureProcessNodeEnv } from '../util.js'; import { ensureProcessNodeEnv } from '../util.js';
import { formatErrorMessage } from '../messages.js';
import { collectErrorMetadata } from '../errors/dev/utils.js';
export type ProcessExit = 0 | 1; export type ProcessExit = 0 | 1;
@ -55,7 +57,16 @@ export default async function sync(
command: 'build', command: 'build',
}); });
return await syncInternal(settings, { ...options, logger }); try {
return await syncInternal(settings, { ...options, logger });
} catch (err) {
const error = createSafeError(err);
logger.error(
'content',
formatErrorMessage(collectErrorMetadata(error), logger.level() === 'debug') + '\n'
);
return 1;
}
} }
/** /**

View file

@ -35,14 +35,7 @@ name: Ben
root root
); );
try { expect(await sync({ fs })).to.equal(1);
await sync({ fs });
expect.fail(0, 1, 'Expected sync to throw');
} catch (e) {
expect(e).to.be.instanceOf(Error);
expect(e.type).to.equal('AstroError');
expect(e.message).to.include('authors');
}
}); });
it('raises "mixed content" error when data in content collection', async () => { it('raises "mixed content" error when data in content collection', async () => {
@ -70,15 +63,7 @@ title: Post
root root
); );
try { expect(await sync({ fs })).to.equal(1);
await sync({ fs });
expect.fail(0, 1, 'Expected sync to throw');
} catch (e) {
expect(e).to.be.instanceOf(Error);
expect(e.type).to.equal('AstroError');
expect(e.message).to.include('blog');
}
}); });
it('raises error when data collection configured as content collection', async () => { it('raises error when data collection configured as content collection', async () => {
@ -101,14 +86,7 @@ title: Post
root root
); );
try { expect(await sync({ fs })).to.equal(1);
await sync({ fs });
expect.fail(0, 1, 'Expected sync to throw');
} catch (e) {
expect(e).to.be.instanceOf(Error);
expect(e.type).to.equal('AstroError');
expect(e.hint).to.include("Try adding `type: 'data'`");
}
}); });
it('does not raise error for empty collection with config', async () => { it('does not raise error for empty collection with config', async () => {