Skip to content

Commit

Permalink
Improve astro sync error handling (#9888)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
natemoo-re and lilnasy committed Feb 1, 2024
1 parent 756f959 commit 9d2fdb2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-jokes-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Improves error handling logic for the `astro sync` command.
2 changes: 1 addition & 1 deletion packages/astro/src/core/errors/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion packages/astro/src/core/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand Down

0 comments on commit 9d2fdb2

Please sign in to comment.