refactor!: split builder config (#154)

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-11-11 00:39:56 +08:00
committed by GitHub
parent 5078143b1e
commit 082f201aea
132 changed files with 1679 additions and 1093 deletions

View File

@@ -741,14 +741,20 @@ export class HonoHttpApplication {
const match = error.message.match(regexp)
if (match) {
const [, dependency, position, constructor] = match
throw new ReferenceError(
`Cannot inject the dependency ${colors.yellow(dependency)} at position #${position} of "${colors.yellow(constructor)}" constructor.` +
`\n` +
`Please check if the dependency is registered in the container. Check import the dependency not the type.` +
`\n${colors.red(`- import type { ${dependency} } from "./service";`)}\n${colors.green(
`+ import { ${dependency} } from "./service";`,
)}`,
)
const baseMessage = `Cannot inject the dependency ${colors.yellow(dependency)} at position #${position} of "${colors.yellow(constructor)}" constructor.`
const guidance =
`\nPlease ensure ${colors.yellow(dependency)} (and its own dependencies) are registered as providers or the module that declares them is imported.` +
`\nIf you are importing only the TypeScript type, switch to a runtime import:` +
`\n${colors.red(`- import type { ${dependency} } from "./service";`)}\n${colors.green(
`+ import { ${dependency} } from "./service";`,
)}`
const rootCause = this.formatInjectionRootCause(error)
const detailedMessage =
baseMessage +
guidance +
(rootCause ? `\n\n${colors.bold('Container details:')}\n${colors.gray(rootCause)}` : '')
throw new ReferenceError(detailedMessage, { cause: error })
}
}
throw error
@@ -756,6 +762,18 @@ export class HonoHttpApplication {
}
}
private formatInjectionRootCause(error: Error): string {
const message = error.message ?? ''
if (!message) {
return ''
}
return message
.split('\n')
.map((line) => line.trim())
.filter((line, index, arr) => line.length > 0 && (index === 0 || line !== arr[index - 1]))
.join('\n')
}
private registerSingleton<T>(token: Constructor<T>): void {
const injectionToken = token as unknown as InjectionToken<T>
if (!this.container.isRegistered(injectionToken, true)) {