0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

add double check on astro file return type to display more human readable error (#4857)

This commit is contained in:
Steven Yung 2022-09-26 20:13:25 +02:00 committed by GitHub
parent e948f62bbc
commit 6fd00c4941
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 2 deletions

View file

@ -6,6 +6,8 @@ import { getProducts } from '../api';
import '../styles/common.css'; import '../styles/common.css';
const products = await getProducts(Astro.request); const products = await getProducts(Astro.request);
return;
--- ---
<html lang="en"> <html lang="en">

View file

@ -112,7 +112,12 @@ export async function renderPage(
let response = createResponse(body, { ...init, headers }); let response = createResponse(body, { ...init, headers });
return response; return response;
} else {
return factoryReturnValue;
} }
// We double check if the file return a Response
if (!(factoryReturnValue instanceof Response)) {
throw new Error('Only instance of Response can be returned from an Astro file');
}
return factoryReturnValue;
} }

View file

@ -0,0 +1,35 @@
import { expect, assert } from 'chai';
import { loadFixture } from './test-utils.js';
// Asset bundling
describe('Not returning responses', () => {
let fixture;
/** @type {import('./test-utils').DevServer} */
let devServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-not-response/',
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('Does not work from a page', async () => {
try {
await fixture.build();
} catch (e) {
expect(e).to.be.instanceOf(
Error,
'Only instance of Response can be returned from an Astro file'
);
return null;
}
assert.fail('Should have thrown an error');
});
});

View file

@ -0,0 +1,8 @@
{
"name": "@test/astro-not-response",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,3 @@
---
return null;
---

View file

@ -1335,6 +1335,12 @@ importers:
dependencies: dependencies:
astro: link:../../.. astro: link:../../..
packages/astro/test/fixtures/astro-not-response:
specifiers:
astro: workspace:*
dependencies:
astro: link:../../..
packages/astro/test/fixtures/astro-page-directory-url: packages/astro/test/fixtures/astro-page-directory-url:
specifiers: specifiers:
astro: workspace:* astro: workspace:*