From fa3078ce9f5eda408340a78c6d275f3e0b2437dc Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Tue, 19 Dec 2023 08:28:04 +0000 Subject: [PATCH] fix: handle middleware loading error (#9458) * fix: handle middleware loading error * remove the try/catch * rethrow error * Update packages/astro/src/core/middleware/loadMiddleware.ts Co-authored-by: Arsh <69170106+lilnasy@users.noreply.github.com> --------- Co-authored-by: Arsh <69170106+lilnasy@users.noreply.github.com> --- .changeset/selfish-rings-occur.md | 5 +++++ packages/astro/src/core/errors/errors-data.ts | 20 +++++++++++++++++++ .../src/core/middleware/loadMiddleware.ts | 10 ++++++---- 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 .changeset/selfish-rings-occur.md diff --git a/.changeset/selfish-rings-occur.md b/.changeset/selfish-rings-occur.md new file mode 100644 index 000000000000..e5722bb95f23 --- /dev/null +++ b/.changeset/selfish-rings-occur.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Correctly handle the error in case the middleware throws a runtime error diff --git a/packages/astro/src/core/errors/errors-data.ts b/packages/astro/src/core/errors/errors-data.ts index 047bb533721c..691162d595af 100644 --- a/packages/astro/src/core/errors/errors-data.ts +++ b/packages/astro/src/core/errors/errors-data.ts @@ -780,6 +780,26 @@ export const LocalsNotAnObject = { hint: 'If you tried to remove some information from the `locals` object, try to use `delete` or set the property to `undefined`.', } satisfies ErrorData; +/** + * @docs + * @description + * Thrown in development mode when middleware throws an error while attempting to loading it. + * + * For example: + * ```ts + * import {defineMiddleware} from "astro:middleware"; + * throw new Error("Error thrown while loading the middleware.") + * export const onRequest = defineMiddleware(() => { + * return "string" + * }); + * ``` + */ +export const MiddlewareCantBeLoaded = { + name: 'MiddlewareCantBeLoaded', + title: "Can't load the middleware.", + message: 'The middleware thrown an error while Astro was trying to loading it.', +} satisfies ErrorData; + /** * @docs * @see diff --git a/packages/astro/src/core/middleware/loadMiddleware.ts b/packages/astro/src/core/middleware/loadMiddleware.ts index 4f524ee52756..135f7fbc479e 100644 --- a/packages/astro/src/core/middleware/loadMiddleware.ts +++ b/packages/astro/src/core/middleware/loadMiddleware.ts @@ -1,5 +1,7 @@ import type { ModuleLoader } from '../module-loader/index.js'; import { MIDDLEWARE_MODULE_ID } from './vite-plugin.js'; +import { MiddlewareCantBeLoaded } from '../errors/errors-data.js'; +import { AstroError } from '../errors/index.js'; /** * It accepts a module loader and the astro settings, and it attempts to load the middlewares defined in the configuration. @@ -8,9 +10,9 @@ import { MIDDLEWARE_MODULE_ID } from './vite-plugin.js'; */ export async function loadMiddleware(moduleLoader: ModuleLoader) { try { - const module = await moduleLoader.import(MIDDLEWARE_MODULE_ID); - return module; - } catch { - return void 0; + return await moduleLoader.import(MIDDLEWARE_MODULE_ID); + } catch (error: any) { + const astroError = new AstroError(MiddlewareCantBeLoaded, undefined, { cause: error }); + throw astroError; } }