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

fix: handle middleware loading error #9458

Merged
merged 4 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/selfish-rings-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Correctly handle the error in case the middleware throws a runtime error
10 changes: 8 additions & 2 deletions packages/astro/src/core/middleware/loadMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import type { ModuleLoader } from '../module-loader/index.js';
import { MIDDLEWARE_MODULE_ID } from './vite-plugin.js';
import type { Logger } from '../logger/core.js';

/**
* It accepts a module loader and the astro settings, and it attempts to load the middlewares defined in the configuration.
*
* If not middlewares were not set, the function returns an empty array.
*/
export async function loadMiddleware(moduleLoader: ModuleLoader) {
export async function loadMiddleware(moduleLoader: ModuleLoader, logger: Logger) {
try {
const module = await moduleLoader.import(MIDDLEWARE_MODULE_ID);
return module;
} catch {
} catch (e: any) {
logger.error(
Copy link
Member

Choose a reason for hiding this comment

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

Is there a specific reason for this to be a log instead of an actual throw?

Copy link
Member Author

Choose a reason for hiding this comment

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

No particular reason. Should we throw an astro error? What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I'd say so! Presumably if the middleware fails to load, we'd like the build to fail? I'd just wonder in dev if it would work correctly? Would it crash the dev server? If so, maybe it'd be possible to handle it correctly using the method Matthew added not too long ago.

'middleware',
"Astro couldn't load the middleware because the module contains an error."
);
logger.error('middleware', e.stack || e.message);
return void 0;
}
}
2 changes: 1 addition & 1 deletion packages/astro/src/vite-plugin-astro-server/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export async function handleRoute({
let mod: ComponentInstance | undefined = undefined;
let options: SSROptions | undefined = undefined;
let route: RouteData;
const middleware = await loadMiddleware(moduleLoader);
const middleware = await loadMiddleware(moduleLoader, logger);

if (!matchedRoute) {
if (config.i18n) {
Expand Down
Loading