fix(framework): throw when middleware fails to register (#9211)

This commit is contained in:
Riqwan Thamir
2024-09-22 14:46:52 +02:00
committed by GitHub
parent ba14d4341f
commit 0bce09f0a2
+23 -34
View File
@@ -10,6 +10,9 @@ import {
} from "express" } from "express"
import { readdir } from "fs/promises" import { readdir } from "fs/promises"
import { extname, join, parse, sep } from "path" import { extname, join, parse, sep } from "path"
import { configManager } from "../config"
import { logger } from "../logger"
import { authenticate, AuthType, errorHandler } from "./middlewares"
import { import {
GlobalMiddlewareDescriptor, GlobalMiddlewareDescriptor,
HTTP_METHODS, HTTP_METHODS,
@@ -25,9 +28,6 @@ import {
RouteHandler, RouteHandler,
RouteVerb, RouteVerb,
} from "./types" } from "./types"
import { authenticate, AuthType, errorHandler } from "./middlewares"
import { configManager } from "../config"
import { logger } from "../logger"
const log = ({ const log = ({
activityId, activityId,
@@ -486,43 +486,32 @@ class ApiRoutesLoader {
const absolutePath = join(this.#sourceDir, middlewareFilePath) const absolutePath = join(this.#sourceDir, middlewareFilePath)
try { await import(absolutePath).then((import_) => {
await import(absolutePath).then((import_) => { const middlewaresConfig = import_.default as MiddlewaresConfig | undefined
const middlewaresConfig = import_.default as
| MiddlewaresConfig
| undefined
if (!middlewaresConfig) { if (!middlewaresConfig) {
log({ log({
activityId: this.#activityId, activityId: this.#activityId,
message: `No middleware configuration found in ${absolutePath}. Skipping middleware configuration.`, message: `No middleware configuration found in ${absolutePath}. Skipping middleware configuration.`,
})
return
}
middlewaresConfig.routes = middlewaresConfig.routes?.map((route) => {
return {
...route,
method: route.method ?? "USE",
}
}) })
return
}
const descriptor: GlobalMiddlewareDescriptor = { middlewaresConfig.routes = middlewaresConfig.routes?.map((route) => {
config: middlewaresConfig, return {
...route,
method: route.method ?? "USE",
} }
this.validateMiddlewaresConfig(descriptor)
this.#globalMiddlewaresDescriptor = descriptor
})
} catch (error) {
log({
activityId: this.#activityId,
message: `Failed to load middleware configuration in ${absolutePath}. Skipping middleware configuration.`,
}) })
return const descriptor: GlobalMiddlewareDescriptor = {
} config: middlewaresConfig,
}
this.validateMiddlewaresConfig(descriptor)
this.#globalMiddlewaresDescriptor = descriptor
})
} }
protected async createRoutesMap(): Promise<void> { protected async createRoutesMap(): Promise<void> {