Skip to content

Commit

Permalink
Error when getImage() is passed an undefined src (#9423)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp authored Dec 13, 2023
1 parent 836ab62 commit bda1d29
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-apricots-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Error when getImage is passed an undefined src
6 changes: 6 additions & 0 deletions packages/astro/src/assets/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ export async function getImage(
message: AstroErrorData.ExpectedImageOptions.message(JSON.stringify(options)),
});
}
if(typeof options.src === 'undefined') {
throw new AstroError({
...AstroErrorData.ExpectedImage,
message: AstroErrorData.ExpectedImage.message(options.src, 'undefined', JSON.stringify(options))
});
}

const service = await getConfiguredImageService();

Expand Down
5 changes: 2 additions & 3 deletions packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,14 +640,13 @@ describe('astro:image', () => {
expect(logs[0].message).to.contain('Expected getImage() parameter');
});

// TODO: For some reason, this error crashes the dev server?
it.skip('properly error when src is undefined', async () => {
it('properly error when src is undefined', async () => {
logs.length = 0;
let res = await fixture.fetch('/get-image-undefined');
await res.text();

expect(logs).to.have.a.lengthOf(1);
expect(logs[0].message).to.contain('Expected src to be an image.');
expect(logs[0].message).to.contain('Expected `src` property');
});

it('properly error image in Markdown frontmatter is not found', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
import { getImage } from "astro:assets";
const myImage = getImage({src: undefined});
const myImage = await getImage({src: undefined});
---

0 comments on commit bda1d29

Please sign in to comment.