Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when getImage() is passed an undefined src #9423

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of a duplicate check from the one in baseService, I do think it's okay though because other services should probably have this check as well.

});
}

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});
---
Loading