chore(modules-sdk): throw when failing to load module (#7431)
This commit is contained in:
committed by
GitHub
parent
e3beaf5a29
commit
7b0cfe3b77
@@ -4,7 +4,6 @@ import {
|
||||
ModuleResolution,
|
||||
} from "@medusajs/types"
|
||||
import { createMedusaContainer } from "@medusajs/utils"
|
||||
import { EOL } from "os"
|
||||
import { moduleLoader } from "../module-loader"
|
||||
|
||||
const logger = {
|
||||
@@ -114,11 +113,9 @@ describe("modules loader", () => {
|
||||
},
|
||||
}
|
||||
|
||||
await moduleLoader({ container, moduleResolutions, logger })
|
||||
|
||||
expect(logger.warn).toHaveBeenCalledWith(
|
||||
`Could not resolve module: TestService. Error: Loaders for module TestService failed: loader${EOL}`
|
||||
)
|
||||
expect(
|
||||
moduleLoader({ container, moduleResolutions, logger })
|
||||
).rejects.toThrow("Loaders for module TestService failed: loader")
|
||||
})
|
||||
|
||||
it("should log the errors if no service is defined", async () => {
|
||||
@@ -142,15 +139,14 @@ describe("modules loader", () => {
|
||||
},
|
||||
}
|
||||
|
||||
await moduleLoader({ container, moduleResolutions, logger })
|
||||
|
||||
expect(logger.warn).toHaveBeenCalledWith(
|
||||
`Could not resolve module: TestService. Error: No service found in module. Make sure your module exports a service.${EOL}`
|
||||
expect(
|
||||
moduleLoader({ container, moduleResolutions, logger })
|
||||
).rejects.toThrow(
|
||||
"No service found in module TestService. Make sure your module exports a service."
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error if no service is defined and the module is required", async () => {
|
||||
expect.assertions(1)
|
||||
const moduleResolutions: Record<string, ModuleResolution> = {
|
||||
testService: {
|
||||
resolutionPath: "@modules/no-service",
|
||||
@@ -172,13 +168,11 @@ describe("modules loader", () => {
|
||||
},
|
||||
}
|
||||
|
||||
try {
|
||||
await moduleLoader({ container, moduleResolutions, logger })
|
||||
} catch (err) {
|
||||
expect(err.message).toEqual(
|
||||
"No service found in module. Make sure your module exports a service."
|
||||
)
|
||||
}
|
||||
expect(
|
||||
moduleLoader({ container, moduleResolutions, logger })
|
||||
).rejects.toThrow(
|
||||
"No service found in module TestService. Make sure your module exports a service."
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error if the default package isn't found and the module is required", async () => {
|
||||
|
||||
@@ -33,16 +33,10 @@ export const moduleLoader = async ({
|
||||
|
||||
if (registrationResult?.error) {
|
||||
const { error } = registrationResult
|
||||
if (resolution.definition.isRequired) {
|
||||
logger?.error(
|
||||
`Could not resolve required module: ${resolution.definition.label}. Error: ${error.message}${EOL}`
|
||||
)
|
||||
throw error
|
||||
}
|
||||
|
||||
logger?.warn(
|
||||
logger?.error(
|
||||
`Could not resolve module: ${resolution.definition.label}. Error: ${error.message}${EOL}`
|
||||
)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,9 +15,9 @@ import {
|
||||
ModulesSdkUtils,
|
||||
} from "@medusajs/utils"
|
||||
import { asFunction, asValue } from "awilix"
|
||||
import { join, resolve } from "path"
|
||||
import { statSync } from "fs"
|
||||
import { readdir } from "fs/promises"
|
||||
import { join, resolve } from "path"
|
||||
|
||||
type ModuleResource = {
|
||||
services: Function[]
|
||||
@@ -90,7 +90,7 @@ export async function loadInternalModule(
|
||||
|
||||
return {
|
||||
error: new Error(
|
||||
"No service found in module. Make sure your module exports a service."
|
||||
`No service found in module ${resolution?.definition?.label}. Make sure your module exports a service.`
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user