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:
parent
756f9595e7
commit
9d2fdb293d
4 changed files with 21 additions and 27 deletions
5
.changeset/healthy-jokes-deny.md
Normal file
5
.changeset/healthy-jokes-deny.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"astro": patch
|
||||
---
|
||||
|
||||
Improves error handling logic for the `astro sync` command.
|
|
@ -27,7 +27,7 @@ type ErrorTypes =
|
|||
| 'AggregateError';
|
||||
|
||||
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 {
|
||||
|
|
|
@ -18,6 +18,8 @@ import { createVite } from '../create-vite.js';
|
|||
import { AstroError, AstroErrorData, createSafeError, isAstroError } from '../errors/index.js';
|
||||
import type { Logger } from '../logger/core.js';
|
||||
import { ensureProcessNodeEnv } from '../util.js';
|
||||
import { formatErrorMessage } from '../messages.js';
|
||||
import { collectErrorMetadata } from '../errors/dev/utils.js';
|
||||
|
||||
export type ProcessExit = 0 | 1;
|
||||
|
||||
|
@ -55,7 +57,16 @@ export default async function sync(
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -35,14 +35,7 @@ name: Ben
|
|||
root
|
||||
);
|
||||
|
||||
try {
|
||||
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');
|
||||
}
|
||||
expect(await sync({ fs })).to.equal(1);
|
||||
});
|
||||
|
||||
it('raises "mixed content" error when data in content collection', async () => {
|
||||
|
@ -70,15 +63,7 @@ title: Post
|
|||
root
|
||||
);
|
||||
|
||||
try {
|
||||
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');
|
||||
}
|
||||
expect(await sync({ fs })).to.equal(1);
|
||||
});
|
||||
|
||||
it('raises error when data collection configured as content collection', async () => {
|
||||
|
@ -101,14 +86,7 @@ title: Post
|
|||
root
|
||||
);
|
||||
|
||||
try {
|
||||
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'`");
|
||||
}
|
||||
expect(await sync({ fs })).to.equal(1);
|
||||
});
|
||||
|
||||
it('does not raise error for empty collection with config', async () => {
|
||||
|
|
Loading…
Add table
Reference in a new issue