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(framework): throw when middleware fails to register #9211

Merged
merged 1 commit into from
Sep 22, 2024
Merged
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
55 changes: 22 additions & 33 deletions packages/framework/framework/src/http/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
} from "express"
import { readdir } from "fs/promises"
import { extname, join, parse, sep } from "path"
import { configManager } from "../config"
import { logger } from "../logger"
import { authenticate, AuthType, errorHandler } from "./middlewares"
import {
GlobalMiddlewareDescriptor,
HTTP_METHODS,
Expand All @@ -25,9 +28,6 @@ import {
RouteHandler,
RouteVerb,
} from "./types"
import { authenticate, AuthType, errorHandler } from "./middlewares"
import { configManager } from "../config"
import { logger } from "../logger"

const log = ({
activityId,
Expand Down Expand Up @@ -486,43 +486,32 @@ class ApiRoutesLoader {

const absolutePath = join(this.#sourceDir, middlewareFilePath)

try {
await import(absolutePath).then((import_) => {
const middlewaresConfig = import_.default as
| MiddlewaresConfig
| undefined

if (!middlewaresConfig) {
log({
activityId: this.#activityId,
message: `No middleware configuration found in ${absolutePath}. Skipping middleware configuration.`,
})
return
}
await import(absolutePath).then((import_) => {
const middlewaresConfig = import_.default as MiddlewaresConfig | undefined

middlewaresConfig.routes = middlewaresConfig.routes?.map((route) => {
return {
...route,
method: route.method ?? "USE",
}
if (!middlewaresConfig) {
log({
activityId: this.#activityId,
message: `No middleware configuration found in ${absolutePath}. Skipping middleware configuration.`,
})
return
}

const descriptor: GlobalMiddlewareDescriptor = {
config: middlewaresConfig,
middlewaresConfig.routes = middlewaresConfig.routes?.map((route) => {
return {
...route,
method: route.method ?? "USE",
}
})

this.validateMiddlewaresConfig(descriptor)
const descriptor: GlobalMiddlewareDescriptor = {
config: middlewaresConfig,
}

this.#globalMiddlewaresDescriptor = descriptor
})
} catch (error) {
log({
activityId: this.#activityId,
message: `Failed to load middleware configuration in ${absolutePath}. Skipping middleware configuration.`,
})
this.validateMiddlewaresConfig(descriptor)

return
}
this.#globalMiddlewaresDescriptor = descriptor
})
}

protected async createRoutesMap(): Promise<void> {
Expand Down
Loading